"Restudying" Build Java Web project from scratch (ii)

Source: Internet
Author: User
Tags java web

Series one describes the basic steps for creating a new Web project, and series two is ready to introduce the basic JSP and servlet usage.

(for JSP compiler instructions, action instructions, built-in objects are not within the scope of this article)

1. First, add the JSP and servlet support packages to the Pom.xml file.

<dependencies>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.1.0</version>

</dependency>

<dependency>

<groupId>javax.servlet.jsp</groupId>

<artifactId>jsp-api</artifactId>

<version>2.2</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

</dependencies>

2. Write the first servlet to process the request.

All servlets must inherit httpservlet in order to have the ability to process and respond to HTTP requests, which is also the necessity of adding above dependencies. The subclass servlet only needs to override the Doget () method to customize the processing of GET requests, overriding the DoPost () method to handle post requests, and other methods such as DoPut (), DoDelete ().

If you are familiar with the knowledge of HttpServlet, you will know that rewriting the service () method also achieves the purpose, because this is the central method of processing the request, which is responsible for processing the request and assigning it to the specified method according to the request type, as shown in:

The first instance of the servlet is given below.

public class HelloServlet extends httpservlet{

@Override

protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {

Request.setcharacterencoding ("UTF-8");

System.out.println (Request.getquerystring ());

String name = Request.getparameter ("name");

String gender = Request.getparameter ("gender");

PrintStream out = new PrintStream (Response.getoutputstream ());

Out.println ("

Out.println ("

Out.println ("<title>servlet test </title>");

Out.println ("

Out.println ("<body>");

Out.println ("Your name is:" + name + "

OUT.PRINTLN ("Your Gender:" + Gender + "

Out.println ("</body>

}

}

This is a classic processing and corresponding method, using the output stream of the response object to spell the page directly and then return it to the client. There are other ways, such as a JSP page, which will be described in detail behind this.

The life cycle of the Ps:servlet and the Init () and Destroy () methods should also be mastered.

3. Configure the requested mappings in Web. xml

Although HelloServlet has the ability to handle requests, it is not configured to handle URL requests that are specific to that style, which is determined in Web. Xml. In the Web-app element of Web. XML, add the following code:

<servlet>

<servlet-name>helloServlet</servlet-name>

<servlet-class>Servlet.HelloServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>helloServlet</servlet-name>

<url-pattern>/hello</url-pattern>

</servlet-mapping>

This way, the HelloServlet is bound to handle the/hello request.

4. Request instance

5. Important points of knowledge

At the beginning of Servlet 3.0, it is convenient to use @WebServlet annotations instead of configuring the request mode in Web. Xml. Demonstrates this, as well as rewriting the service () method.

In fact, after the Servlet 2.5 specification, the necessity of Web. XML is no longer there, probably because of the strong rise of annotations. However, we are still used to preserving this web. XML when we develop it, and then we are taking advantage of annotations at the same time.

@WebServlet (name= "Worldservlet", Urlpatterns = {"/world"})

public class Worldservlet extends HttpServlet {

@Override

public void Service (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {

Request.setcharacterencoding ("UTF-8");

System.out.println (Request.getquerystring ());

String World = Request.getparameter ("World");

PrintStream out = new PrintStream (Response.getoutputstream ());

Out.println ("

Out.println ("

Out.println ("<title>world test </title>");

Out.println ("

Out.println ("<body>");

OUT.PRINTLN ("Your World is:" + Worlds + "

Out.println ("</body>

}

}

In addition to writing directly back to the output page in the servlet's processing method, you can also respond to requests in a JSP manner. In fact, JSP is a special servlet, it is compiled by the Web container (such as Tomcat) generated servlet, interested in the data can be consulted, specifically the way the page jumps, using Request.forward ("/process.jsp ") method. (There are also redirects, developers still need to distinguish between the two, not mentioned here)

The Servlet code looks like this:

@WebServlet (name = "Thirdservlet", Urlpatterns = {"/third"})

public class Thirdservlet extends HttpServlet {

@Override

public void Service (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {

Request.getrequestdispatcher ("/web-inf/page/process.jsp"). Forward (Request,response);

Request.getrequestdispatcher ("/outer_process.jsp"). Forward (Request,response);

}

}

The process.jsp content is as follows, placed under/web-inf/page, and has a file with the same content (except title) on the outside Outer_ Process.jsp, this is to demonstrate their differences when using forward, because files under Web-inf cannot be accessed directly from the browser.

<%@ page contenttype= "Text/html;charset=utf-8" language= "java"%>

<title> Processing Pages </title>

<body>

<!--use the JSP built-in object out (javax.servlet.jsp.JspWriter) to output the current time and refresh the updatable time. -

<%

Out.println (New Java.util.Date ());

%>

<!--use JSP built-in object request to get parameters--

<%

String name = Request.getparameter ("name");

Integer age = Integer.parseint (Request.getparameter ("Age"));

%>

Your name is: <%=name%>

Your age is:<%=age%>

</body>

"Restudying" Build Java Web project from scratch (ii)

Related Article

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.