jsp-built-in object request, response configuration servlet

Source: Internet
Author: User

1. Accessing Dynamic Web pages

To use URLs for page access:

URL: Uniform Resource Locator, which is often said URL;

URLs are the only naming conventions that can identify specific computers, directories, or folders on the Internet;

Composition: The first part: agreement

The second part: the host's IP address, the host or the port number, is each computer's unique identification;

The third part: the address of the project resource, such as directory and folder name.

2. Comments in the JSP file

① write Java scripts in JSP using <%%> to surround them

② in JSP if you want to output an expression using <%=%>

③ in JSP if you want to define a method that uses <%! %>

④ in JSP if you only comment on HTML tags use <!----> comments, if you want to annotate all code like Java scripts, use JSP annotations <%--%>

3. JSP built-in object-request, response

The request is primarily used to pass values between pages.

// before obtaining the request data, it must be written, whether or not Chinese, in case garbled request.setcharacterencoding ("Utf-8"); // Get user name String username=request.getparameter ("UserName"); // Get Password String password=request.getparameter ("PassWord"); // Get Sex String sex=request.getparameter ("Sex"); // Get a hobby String hobby[]=request.getparametervalues ("hobby");

There are two ways to 3.1 a Java jump page:

Redirect: Once a new request is sent, the address bar will change.

Response.sendredirect ();

// manually passing values in the Address bar to get the value, but this method is not suitable for the value of the transfer, when the value is not required to use. response.sendredirect ("page/welcome.jsp?username=" +username+ "&password=" +password);

Forwarding: No new requests will occur, the original request is forwarded directly to the final page, and the address bar does not change.

Request.getrequestdispatcher ();

// forwarded, used to pass a value. request.getrequestdispatcher ("welcome.jsp"). Forward (request, response);
  "The difference between forwarding and redirection"

1) Redirect: Use when no value is required. Occurs on the client, the address bar changes (because the request was re-initiated, the original request parameters will not be brought past).

2) Forwarding: used when the value of the transfer is required. Occurs on the server side, the address bar does not change, the original request parameters meeting and bring the past.

4. Configuring the servlet in Web. XML

1) The configuration is as follows:

<!--configuration Welcome Start Page--><welcome-file-list>   <welcome-file>index.jsp</welcome-file></ welcome-file-list><!--Configuring the servlet--><servlet>  <servlet-name>logincheck</servlet-name >  <!--the name can be picked up here, but it's best to be able to read it--  <servlet-class>com.jredu.servlet.logincheck </servlet-class>  <!--is Logincheck.java package path--   <load-on-startup>1</ load-on-startup><!--The servlet is initialized immediately when the server is started, the parameter must be a number 1, the number represents the priority--></servlet><servlet-mapping >  <servlet-name>logincheck</servlet-name>  <!--the same name as above--  <url-pattern >/loginCheck</url-pattern>  <!--/random name, but jump is the package path above--></servlet-mapping>

2) The life cycle of the servlet three methods:

Init (): servlet initialization

Service (): Servlet is serving

Distory (): The servlet was destroyed.

3) When you need to pass/fetch values, if you need to pass a single value or an array, when you need to pass a list, by Request.setatribute () to pass the value, and then through Request.getatribute () to take the value.

jsp-built-in object request, response configuration servlet

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.