A re-summary of the servlet (boutique)

Source: Internet
Author: User

You can first access the servlet through a URL on the browser side, because the URL is mapped to the class full name in Web. Xml.

When we were beginners, it was easy to tell whether the browser was accessing a JSP page or a servlet. In fact, when we use a browser to access the servlet,

Servlets generally have only two ways:

The first , the simplest, generally applies only to test tests. is to output an HTML page with an Out object. The essence is Out.print (""); the method can output content on the browser side. (like outputting content in the background)

And a very important point is that this method can output HTML tags. Output it as an HTML page. and is accessed by default using the Doget method. (cause unknown??)

Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classHelloServletextendsHttpServlet {/*** Constructor of the object. */     PublicHelloServlet () {Super(); }    /*** Destruction of the servlet. <br>*/     Public voiddestroy () {Super. Destroy ();//Just puts "destroy" string in log//Put Your code here    }    /*** The Doget method of the servlet. <br> * * This method was called when a form have its tag value met     Hod equals to get. *      * @paramrequest the request send by the client to the server *@paramresponse The response send by the server to the client *@throwsservletexception If an error occurred *@throwsIOException If an error occurred*/     Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//Response.setcontenttype ("text/html");PrintWriter out =Response.getwriter (); //out.println ("<! DOCTYPE HTML public \ "-// the//DTD HTML 4.01 Transitional//en\ ">"); //out.println ("<HTML>"); //out.println ("//out.println ("<BODY>");Out.print ("This is"); Out.println ("Doget () <br>"); Out.print ( This. GetClass ()); Out.println (", using the GET method"); Out.println ("</BODY>"); Out.println ("</HTML>");        Out.flush ();    Out.close (); }    /*** The DoPost method of the servlet. <br> * * This method was called when a form have its tag value me     Thod equals to post. *      * @paramrequest the request send by the client to the server *@paramresponse The response send by the server to the client *@throwsservletexception If an error occurred *@throwsIOException If an error occurred*/     Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcontenttype ("Text/html"); PrintWriter out=Response.getwriter (); Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > "); Out.println ("<HTML>"); Out.println ("); Out.println ("<BODY>"); Out.print ("This is"); Out.print ("Why not?"); Out.print ( This. GetClass ()); Out.println (", using the POST method"); Out.println ("</BODY>"); Out.println ("</HTML>");        Out.flush ();    Out.close (); }    /*** Initialization of the servlet. <br> * *@throwsServletexception If an error occurs*/     Public voidInit ()throwsservletexception {//Put Your code here    }}

When outputting HTML tags, this page is equivalent to a JSP page, because the JSP page is composed of This also makes it unclear whether the visit is a servlet or a JSP page.

And the URL of the access servlet is set in the map of the Web. xml file, there is a default URL(from/To, this/cannot save), so when accessing the servlet class separately, you need to output the URL of the project in the browser plus this URL.

The second is the most important role of the servlet.

Implement a jump to the JSP page in the servlet and pass the value.

However, in theory a browser page JSP page on the above click an action, corresponding to throw submitted to a URL connection. (then this URL is a servlet map corresponding to the URL, is not the absolute path of the URL??)

Pass to a servlet class to handle this page action. (And when submitting the Doget method to access the servlet, or the Dopost method, and select whether the form pass parameters)

So this servlet class, the task is to handle this page operation, call the corresponding service processing, and return parameters (this is how to do, it seems that there is no argument after processing, but the database data changed, the final result when you re-jump JSP page, this page is very important, To show the results page of the implementation of the first action, there should be only a small change to the first JSP page. So it's called a JSP page that re-views the database, which is the original page.

Then there is a question: Why did the initial JSP page have a query function on the database? is because the JSP page that we first visited is actually the result of accessing the servlet to implement the database scan the last jump of the JSP page????

Yes, very clever. Because of the simple JSP page, it is impossible to read the database information. So we start by doing the reading of the database data, that is, the first time we visit this site, the real access to a servlet URL. In the servlet, the operation that reads the database data is processed, and the accessed data is stored in memory, as parameters are passed to the JSP page where the servlet last jumps.

Here, there is another important issue, that is how to accept the JSP in the servlet result parameters.

When processing a traverse, the teacher does not save the parameter to the session, but instead saves it to the Req.setattribute attribute.

// to pass a value            to a page Req.setattribute ("command", command);            Req.setattribute ("description", description);             // These two parameters do not pass, because this is the only use, the other layers as parameters to pass out                         Req.setattribute ("Messagelist", listservice.query (command, description));        

Finally, the REQ is treated as a parameter??? To pass a JSP page (Another important thing is that this JSP is only the absolute path, is a kind of path, how many ways??)

Req.getrequestdispatcher ("/web-inf/jsp/back/list.jsp"). Forward (req,                resp);

So how does the JSP page accept this parameter and pass the value to the JSP page and represent it?

It seems that the JSP page uses the jquery method or the Ognl method to call the Java object directly (that is, the parameter is the Java object? or the properties of the class)????

Another important question is why the conditional query and query are mixed together. With an action servlet, is this simplified, high performance?

A re-summary of the servlet (boutique)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.