Combing Servlet and JSP (1), servletjsp

Source: Internet
Author: User
Tags response code

Combing Servlet and JSP (1), servletjsp

I had a JSP course in the first semester of my sophomore year, but I had a small JSP project before the start of school, so I didn't listen to the course for one semester until the final exam score came out, I can only recall the amount of JSP content I still remember. I didn't expect it to be too vague to remember, so I had to go back and learn it. Next, we will sort out Servlet and JSP.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------

A Servlet is a Java program. A Servlet application has one or more Servlet programs, and the JSP page is also converted and compiled into a Servlet program.

The Servlet application cannot run independently and must run in the Servlet container. The Servlet container passes user requests to the Servlet application and returns the results to the user. Most Servlet applications contain multiple JSP pages, so "Servlet/JSP application" is more accurate ".

Among them, Servlet API is the main technology for developing Servlet. WhileServlet APIThere are four Java packages:

JavaPackage

Included content

Javax. servlet

Define the contract classes and interfaces between Servlet and Servlet containers

Javax. servlet. http

Define contract classes and interfaces between HTTP Servlet and Servlet containers

Javax. servlet. annotation

Annotation of Servlet, Filter, and Listener. It is also marked with the original definition metadata

Javax. servlet. descriptor

Type of configuration information that provides programmatic login to web applications.

The core of Servlet technology is Servlet, which is an interface that must be directly or indirectly implemented by all Servlet classes.The Servlet interface defines the contract between the Servlet and the Servlet container.. This contract comes down to the fact that the Servlet container loads the Servlet class into the memory and calls a specific method on the Servlet instance. When a user requests a Servlet container to call the Servlet Service method, a ServletRequest instance and a ServletResponse instance are passed in. The ServletRequest encapsulates the current HTTP request, servletResponse indicates the HTTP Response of the current user. For each application, the Servlet container also creates a ServletContext instance, which encapsulates the environment details of the context (application. Each context has only one ServletContext, and each Servlet instance has a configured ServletConfig.

 

 Servlet lifecycle method:

  

ServletContainer lifecycle Method

Function

Init

When the Servlet is requested for the first time, the Servlet will call this method and no longer call it. Therefore, you can use this method for initialization. When this method is called, The Servlet container will pass in a ServletConfig.

Service

Each time a Servlet is requested, the Servlet container will call this method. When the Servlet is requested for the first time, the Servlet container calls the init method and Service method. Subsequent requests will only call the Service method.

Destroy

When the Servlet is to be destroyed, the Servlet container will call this method. In this method, code cleanup is usually written.

The following describes two other non-lifecycle methods in a Servlet:

ServletContainer non-lifecycle Method

Function

GetServletInfo

This method will return the Servlet description

GetServletConfig

This method will return the ServletConfig passed by the Servlet container to the init method.

However, in order for getServletConfig to return a non-null value, the ServletConfig passed to the init method must be assigned to a class-level variable.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------

The following describes Servlet interfaces:

  ServletRequest

 

Interface

Description

ServletRequest

For each HTTP request, the Servlet container creates a ServletRequest instance and passes it to the Servlet Service method. ServletRequest encapsulates information about this request.

Common methods include:

    

Method

Description

Public int getContentLength ()

Returns the number of bytes of the Request body.

Public java. lang. String getContentType ()

The MIME type of the Request body.

Public java. lang. String getParameter (java. lang. String name)

Returns the value of the specified request parameter.

Public java. lang. String getProtocol ()

The name and version of the HTTP request.

  ServletResponse

Interface

Description

ServletResponse

This interface indicates a Servlet response. Before calling the Servlet Service method, the Servlet container first creates a ServletResponse and passes it as the second parameter to the Service method. ServletResponse hides the complex process of sending responses to browsers.

Common Methods:

 

Method

Description

GetWriter ()

Returns a java. io. PrintWriter that can send text to the client. By default, PrintWriter objects use ISO-8859-1 encoding.

SetContentType ("type ")

Set the response content type and pass "text/html" as a parameter. If the corresponding content type is not set, Some browsers will display the HTML tag as plain text.

  ServletConfig

  

Interface

Description

ServletConfig

Used to store Servlet configuration information.

When the Servlet container initializes the Servlet, the Servlet container will pass in a ServletConfig for the Servlet init () method. The ServletConfig encapsulation can pass the Servlet configuration information through @ WebServlet or descriptor.

Common Methods:

 

Method

Description

Java. lang. String getInitParameter (java, lang. String name)

To obtain the initial parameter value from the Servlet, call the getInitParameter method in the ServletConfig Of The init method that the Servlet container sends to the Servlet.

Java. util. Enumration <java. lang. String> getInitParameterNames ()

Returns an Enumeration of all initial parameter names.

String contactName = servletConfig. getInitParameter ("contactName ")

Obtain the contactName parameter value

  ServletContext

Interface

Description

ServletContext

Each Web application has only one context, so ServletContext can store this context, and ServletContext can also share the information accessed from all the information in the application, and can dynamically register Web objects, in addition, it is saved using the Map in the ServletContext.

Common Methods:

 

Method

Description

GetServletContext (). getInitParameter (String name)

Obtain the initialization parameters for setting context in the web. xml file of the project.

This. getServletContext (). log ("test ")

In the web. xml file, use the logger element to set the log file

GetAttribute (String name)/get AttributeNames ()

Get attributes in ServletContext

SetAttribute (String name, Object object)

Set attributes in ServletContext

RemoveAttribute (String name)

Remove attributes from ServletContext

  GenericServlet

Interface

Description

GenericServlet

GenericServlet implements the Servlet and ServletConfig interfaces. Assign ServletConfig In the init method to a class-level variable, so that it can be obtained through getServletConfig, provide default implementation for all methods in the Servlet interface, and provide methods including ServletConfig.

  Http Servlets

    

Interface

Description

HttpServlet

The HttpServlet class covers the javax. servlet. GenericServlet class. When using HttpServlet, you must also use the HttpServletRequest and HttpServletResponse objects that represent the Servlet request and Servlet response. The difference between HttpServlet and GenericServlet is that HttpServlet covers the doGet or doPost methods instead of the Service method, and uses HttpServletRequest and HttpServletResponse instead of ServletRequest and ServletResponse.

HttpServletRequest

Servlet request in HTTP Environment

HttpServletResponse

Servlet response in HTTP Environment

Common methods for each interface are as follows:

  

Interface

Method

Description

HttpServletRequest

Java. lang. String getContextPath ()

Returns the request uri of the request context.

HttpServletRequest

Cookie [] getCookies ()

Returns an array of Cookie objects.

HttpServletRequest

Java. lang. String getHeader (java. lang. String name)

Returns the value of the specified HTTP header.

HttpServletRequest

Java. lang. String getMethod ()

Returns the HTTP method name that generates the request.

HttpServletRequest

Java. lang. String getQueryString ()

Returns the query string in the request URL.

HttpServletRequest

HttpSession getSession ()

Returns the session object associated with the request. If not, a new session object is created.

HttpServletRequest

HttpSession getSession (Boolean create)

Returns the painting object related to this request. If the create parameter is set to True, a new session object is created.

HttpServletResponse

Void addCookie (Cookie cookie)

Add a cookie to the response object.

HttpServletResponse

Void addHeader (java. lang. String name, java. lang. String value)

Add a header to the response object.

HttpServletResponse

Void sendRedirect (java. lang. String location)

Send a response code to jump the browser to the specified location.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------

When the user submitsHTML formThe value entered in the form element is sent to the server as a request parameter. The values of HTML input fields (text fields, hidden fields, or password fields) or encryption fields are sent as strings to the server. If the input field or region is empty, an empty string is sent.

The select element that contains multiple values sends out a string array, which can be processed through ServletRequest. getParameterValues.

The checked check box will send the string "on" to the server. Unverified check boxes will not send any content to the server. ServletRequest. getParameter (fieldName) will return null.

A single worker sends the value of the selected button to the server. If no button is selected, nothing is sent to the server, and ServletRequest. getParameter (fieldName) returns null.

If a form contains multiple elements with the same input name, all values are submitted and must be obtained using ServletRequest. getParameterValues. ServletRequest. getParameter returns only the last value.

  

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.