Using Connector/J with GlassFish
2115
writer.println("<p>Population: "+rs.getString("Population")+"</p>");
}
}
writer.println("</body></html>");
writer.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp){
try {
resp.setContentType("text/html");
PrintWriter writer = resp.getWriter();
writer.println("<html><body>");
writer.println("<p>Hello from servlet doGet()</p>");
writer.println("</body></html>");
writer.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
In the preceding code a basic
doGet()
method is implemented, but is not used in the example.
The code to establish the connection with the database is as shown in the previous example,
Section 20.3.14.1, “A Simple JSP Application with Glassfish, Connector/J and MySQL”
, and is most
conveniently located in the servlet
init()
method. The corresponding freeing of resources is located
in the destroy method. The main functionality of the servlet is located in the
doPost()
method. If the
user enters nto the input form a country name that can be located in the database, the population of the
country is returned. The code is invoked using a POST action associated with the input form. The form
is defined in the file
index.html
:
<html>
<head><title>HelloWebServlet</title></head>
<body>
<h1>HelloWebServlet</h1>
<p>Please enter country name:</p>
<form action="HelloWebServlet" method="POST">
<input type="text" name="country_name" length="50" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
The XML files
web.xml
and
sun-web.xml
are as for the example in the preceding section,
Section 20.3.14.1, “A Simple JSP Application with Glassfish, Connector/J and MySQL”
, no additional
changes are required.
Whe compiling the Java source code, you will need to specify the path to the file
javaee.jar
. On
Windows, this can be done as follows:
shell> javac -classpath c:\glassfishv3\glassfish\lib\javaee.jar HelloWebServlet.java
Once the code is correctly located within its directory structure, and compiled, the application can be
deployed in Glassfish. This is done in exactly the same way as described in the preceding section,
Section 20.3.14.1, “A Simple JSP Application with Glassfish, Connector/J and MySQL”
.
Once deployed the application can be launched from within the Glassfish Administration Console.
Enter a country name such as “England”, and the application will return “Country does not exist!”. Enter
“France”, and the application will return a population of 59225700.
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...