The servlet that <Java> is almost forgotten

Source: Internet
Author: User

Objective

As a Java web Developer, mastering Sshi and other frameworks is a necessary skill, but with more frames you have to look back at the most primitive servlet. After all, the MVC Framework (STRUTS1/2,SPRINGMVC) is rooted in it.

Building a servlet Project

So first to recall the knowledge of the servlet, not to read, the best direct way is to open the IDE directly to build a simple project.

1. Open the IDE to create a new Web project, in general, as long as your IDE has a JDK and Tomcat does not need to introduce any JAR package (not ready to involve any database operations).

2. Create a new Web. xml file under Web-inf, first write the schema, do not know how to write directly online copy.

3. Start writing servlets, specifically to write their own xxservlet extends HttpServlet, and override two necessary methods Doget and dopost can be, the heart of the students can be service, destroy, The Init method is simple to implement, preferably under the console print to understand the order in which these methods are executed.

4. After writing the servlet, to go to just a configuration of the Web. XML, that is, servlet+servlet-mapping, this step has a little attention is servlet-mapping in the Url-pattern, To know what pattern matches what URL. There are about three kinds:

Exact match, not interpreted as: "/test/list.do".

Ii

Directory matching: That is, "/" or "/*" or "/test/*" at the end of the *.

Extended match: "*.do" starting with a *.

Another thing to note is Load-on-startup this value can generally not write, do not write the words, Tomcat will be used to the time to instantiate, and write a number greater than 0 will be in Tomcat when the instance of the start of the session, So generally only the servlet that needs to be used when Tomcat starts is written, For example, the default configuration of Org.apache.catalina.servlets.DefaultServlet and Org.apache.jasper.servlet.JspServlet in Web. XML is two Servlets, 1 and 3, respectively.

The trouble with this configuration is that each servlet will come here to configure once, which is tedious.

5. Start to write JSP file, there has been a tangled place, that is, the JSP file is placed under the Webroot or under the Web-inf, in fact, this is not stipulated, placed under the Web-inf more secure, direct full path access will be reported 404 errors, It is convenient to put under the Webroot, the security aspect uses the filter to configure the permission. When you write a JSP, you generally know to add a sentence to your head.

<% @ Page ContentType = " Text/html;charset=utf-8 " language = " Java " %>

The meaning of this sentence is the context type of the current page and the encoding format, and Java is supported. The rest is almost as much as the HTML.

6. These steps are ready to start the project, quite simple.

Jump for Servlets and JSPs

One, send request from JSP to servlet (such as Request.getcontextpath () value is/appname)

①, full path access, such as:/appname/index.jsp

Second, jump from the servlet JSP

①, Request.getrequestdispatch ("index.jsp"). ForWord (req, res);//relative path

②, Response.sendredirect (Requst.getcontextpath () + "/index.jsp");//absolute path

Parameter passing and El expression

One, JSP passed to Servlet

①, url delivery, such as List.do?limit=10,servlet, is obtained by Request.getparamter ("limit")

②, form delivery, servlet is also obtained through Request.getparamter ("XX")

Second, servlet passed to JSP

①, Request.setattribute ()

②, Session.setattribute ()

Third, the JSP receives the parameter

①, <%= session.getattribute ()%> or <%= request.getattribute ()%>

②, El expression: such as ${info}. It will look in the page, request, session, application, then return, and return null is not found. <%@ page iselignored= "true"%> can enable disabling of El Expressions and is enabled by default.

  

Deep servlet and Thread safety

Servlets are single-threaded, meaning initialization (init: You can pass in a ServletConfig object and then get it with Getinitparamter) once, and then, if you want to use its service, Tomcat will find it to use instead of creating a new one. Then this requires that we pay attention to thread safety when implementing the servlet. When Tomcat wants to destroy (destroy) it, pay attention to releasing resources in Destroy.

Here's how to make sure that the servlet is thread safe.

①, the first point of Init is Tomcat execution, it is single-threaded, so is thread-safe, so long as the consideration is service ();

If only local variables (that is, local variables and parameters) are involved in the ②, the service method is thread-safe, why is it that each thread of the JVM creates a stack frame for the method and presses it into the stack each time a method is executed, that is, the local variables within the stack frame are not shared by the thread. So, thread safe.

③, if the service method is accessed (the access here refers to read and write, if you just read it then do not need to worry about thread safety issues) Three kinds: servlet member variables, global static variables, global static resources (system files, etc.), You need to add the synchronization control statement (this is not expanded here ...). Actually, I didn't use it myself. Escape).

④, destroy, and Init are single-threaded, and will be destroyed when the server restarts or when it is not used for a long time.

The servlet that <Java> is almost forgotten

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.