Chapter 7 Java Servlet (a)
Hello.java:
}
to easily debug servlet, book, servlet bytecode files stored in D: \ Tomcat \ jakarta-tomcat-4.0 \ webapps \ example \ Web-i nf \ classes, then in the JSP page to call the servlet, servlet URL is:
out.println (header2);
out.println (“
Accept servlet:”);
/ / Get the server name:
import java.util .*;
out.println (“
Protocol:”);
strong> 7.3 .1 strong> strong> through the form to submit data to the servlet
out.println (serverPort);
public void doPost (HttpServletRequest request, HttpServletResponse response)
Example 2
int number = 0;
String serverName = request.getServerName ();
response.setContentType (“text / html; charset = GB2312″); / / set the response MIME type.
strong> us in the JSP page, click on a hyperlink to access servlet.
{
7.1.2 strong> init strong> method
{
public synchronized void doPost (HttpServletRequest request, HttpServletResponse response )
import javax.servlet.http .*;
public void doPost (HttpServletRequest request, HttpServletResponse response)
public void init (ServletConfig config) throws ServletException
{/ / get a send data to the client output stream:
example 8
import javax.servlet.http .*;
out.println (“ BODY> “);
BODY>
out.println (” BODY> “);
submit the name of the JSP page registered
out.println (” body> html> “);
{hashtable.put (person_name, person_name);
out.println (”
client IP: “);
}
if servlet has a package name, for example, Hello package name is tom.jiafei, then call the servlet URL is:
throws ServletException, IOException
public void init (ServletConfig config) throws ServletException
import java.util .*;
import javax.servlet.http .*;
strong> any directory of a Web service JSP page can be accessed through a hyperlink to a form or servlet. Servlet by the JSP page to access the benefits, JSP pages, static pages can be responsible for information processing, dynamic information processing to the servlet to complete.
strong> We already know that, after the servlet is loaded when the servlet request follow-up customer service, the engine will start a new thread in the thread, servlet service method is called to respond to customer requests, and defined in the servlet class member variables, shared by all the client threads. In the following example 4, the use of shared variables to implement a counter.
for (int i = 1; i <= n; i )
out.print (“
” “your name is” person_name);
In the following example 6, the user can submit two forms to the servlet, a positive number, which is a form of presentation post, way to another form of get. Regardless of the user in that way, the server servlet are all factors to calculate this number, returned to the user. 7 In the following example, if using the post method to submit positive, servlet calculate the number of all factors, if using the get method, servlet find less than the total number of primes.
throws ServletException, IOException
/ / text box text information submitted:
break;
strong> HttpServlet except init, service, destroy methods this class has two important methods: doGet and doPost, for handling customer requests and respond to.
out.println (““);
/ examples / servlet / servletName 2 in the following example, JSP page form to submit a positive number, servlet is responsible for calculating the square root of this number.
{out.print (“
” “there are exists illegal letters in your email”);
doPost (request, response);
{super.init (config) ;
response.setContentType (“text / html; charset = GB2312″); OutputStream ();
7.1.3 strong> service strong> method strong>
out.println (“
ServerPort:”);
public class GetMessages extends HttpServlet
BODY>
out.println ( i);
strong> who learned of the Java language Java Applet (Java applets) are familiar with a Java Applet is java.applet.Applet subclass, the subclass of objects by the the client browser is responsible for initializing and running. Applet and servlet to run a similar mechanism, but it runs on the server side. A servlet javax.servlet package is a subclass of HttpServlet class, the servlet from the server to support the completion of the sub-class of objects that the servlet initialization.
double n = 0;
}
client JSP page by clicking a hyperlink to access a computing servlet
public void service (HttpServletRequest request HttpServletResponse response) throw
{
servlet source files (results shown in Figure 7.6)
number ;
}
{super.init (config);
out.println (“ BODY>“);
}
}
import javax.servlet.http .*;
out.println (“you are” count “th” “people”);
out.println (path);
{public void init (ServletConfig config) throws ServletException
{
response.setContentType (“text / plain “);// set the response MIME type to plain text.
public void init (ServletConfig config) throws ServletException
response.setContentType (“text / html; charset = GB2312″); / / set the response MIME type.
{
if often debugging servlet, the servlet can be placed in webapps / example / Web-inf / classes. Note that, when the user requests a servlet service, due to the engine every time the server checks whether the byte code file has been modified, resulting in less efficient operation of the server.
<% @ page contentType = "text / html; charset = GB2312"%>
out.println (““);
}
{ super.init (config);
In the following example, JSP page through a form to submit a positive real number servlet, servlet is responsible for calculating the square root of this number returned to the customer.
out.println (“Now PI =” 4 * sum);
init method is called only once, at the request servlet is loaded when the first call this method. When a subsequent client requests servlet services, Web services will start a new thread in the thread, servlet service method is called to respond to customer requests, that is, each customer every request led to the implementation of service method is called .
catch (NumberFormatException e)
}
LoginByServlet.java:
Enumeration enum = request.getParameterNames ();
Servlet The life cycle is essentially composed of the following three processes:
import javax.servlet .*;
}
out.println (“ HTML>“);




< h3> Xu Qing excellent Yadeng customized fashion magazine tells the story behind the brand (Figure) h3> li>





Video: Zhang Guoqiang big New Year wish friends To Be Number One
< br /> li>
h2>
h2>