HttpServletRequest, request common methods, requests application, forwarding, RequestDispatcher

Source: Internet
Author: User
Tags http request port number

The HttpServletRequest object represents the client's request, and when the client accesses the server through the HTTP protocol, all the information in the HTTP request header is encapsulated in the object, and the developer can obtain this information from the client by means of this object.

Request Common methods

Obtaining Client Information

The Getrequesturl method returns the full URL of the client when the request is made.

The Getrequesturi method returns the resource name portion of the request row.

The GetQueryString method returns the parameters section in the request line.

The Getremoteaddr method returns the IP address of the client that made the request

The Getremotehost method returns the full host name of the client that made the request

The Getremoteport method returns the network port number used by the client

The Getlocaladdr method returns the IP address of the Web server.

Getlocalname method returns the host name of the Web server

GetMethod How to get the client request

Package com.hbsi.request;

Import java.io.IOException;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

public class Request1 extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

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

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

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

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

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

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

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

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

}

public void DoPost (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Doget (request, response);

}

}

Get the client request header

GetHead (name) method

Getheaders (String name) method

Getheadernames method

Get client request parameters (client-submitted data)

GetParameter (name): Gets the parameter value for the specified name. This is one of the most commonly used methods.

Getparametervalues (String name): Gets an array of all values for the specified name parameter. It applies to cases where a parameter name corresponds to multiple values. such as the check box in the page form, the value of the multiple-selection list submission.

Getparameternames (): Returns a enumeration object that contains all the parameter names in the request message. By traversing the enumeration object, you can get all the parameter names in the request message.

Getparametermap (): Returns a Map object that holds all the parameter names and values in the request message. The key of the map object is the parameter name of the string type, and value is an array of values for the object type corresponding to this parameter.

Package com.hbsi.request;

Import java.io.IOException;

Import java.util.Enumeration;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

public class Request2 extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

String value = Request.getheader ("Accept");

System.out.println (value);

Enumeration E = Request.getheadernames ();

while (E.hasmoreelements ()) {

String name = (string) e.nextelement ();

String value = Request.getheader (name);

System.out.println (name+ ":" +value);

}

}

public void DoPost (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Doget (request, response);

}

}

Request Common applications

Acquisition of various form entry data

text, password, radio, checkbox,

File, select, textarea, Hidden,

Image, Button for JS programming

Here is the request code

Package com.hbsi.request;

Import java.io.IOException;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

public class Reqeust3 extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

System.out.println (Request.getparameter ("name"));

}

private void Test1form (HttpServletRequest request) {

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

String Password = request.getparameter ("password");

String sex = request.getparameter ("Sex");

String City = Request.getparameter ("city");

String [] likes = Request.getparametervalues ("likes");

String likestring= "";

for (int i=0;likes!=null && i<likes.length;i++) {

String like = Likes[i];

Likestring +=like;

}

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

System.out.println (name+ "," +password+ "," +sex+ "," +city+ "," +likestring+ "," +memo ");

}

public void DoPost (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Doget (request, response);

}

}

Chinese garbled problem in request parameter

Encoding of the URL address

Package com.hbsi.request;

Import java.io.IOException;

Import java.io.UnsupportedEncodingException;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

public class Request4 extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

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

System.out.println (New String (Value.getbytes ("iso8859-1"), "UTF-8"));

}

private void Test2 (HttpServletRequest request)

Throws Unsupportedencodingexception {

Get mode

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

name = new String (name.getbytes ("iso8859-1"), "UTF-8");

SYSTEM.OUT.PRINTLN (name);

}

private void Test1 (HttpServletRequest request)

Throws Unsupportedencodingexception {

Request.setcharacterencoding ("UTF-8");//only works on Post mode

System.out.println (Request.getparameter ("username"));

}

public void DoPost (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Doget (request, response);

}

}

Anti-theft chain

Package com.hbsi.request;

Import java.io.IOException;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

public class Request5 extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

String referer = Request.getheader ("Referer");

if (Referer==null | |!referer.startswith ("http://localhost")) {

Response.sendredirect ("/servletdemo/index.jsp");

Return

}

Response.setcontenttype ("Text/html;charset=utf-8");

String data = "secret ...." ";

Response.getwriter (). write (data);

}

public void DoPost (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Doget (request, response);

}

}

Request object Implementation requests forwarding: When a Web resource receives a client request, notifies the server to invoke another Web resource for processing.

Request Forwarding

A RequestDispatcher interface, commonly known as a request dispatcher, is defined in the Servlet API. It defines the following two methods:

public void forward (ServletRequest request, servletresponse response)

public void include (ServletRequest request, servletresponse response)

There are two main ways to get RequestDispatcher instances:

Invokes the Getrequestdispatcher (String URL) method provided by the ServletContext interface.

Invokes the Getrequestdispatcher (String URL) method provided by the ServletRequest interface.

RequestDispatcher

Include Method:

The Requestdispatcher.include method is used to include the resource content encapsulated by the RequestDispatcher object as part of the current response content, thus enabling the programmable server-side inclusion functionality.

The included Servlet program cannot change the status code and the response header of the response message, and if there is such a statement, the execution results of these statements are ignored.

Package com.hbsi.request;

Import java.io.IOException;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse;

public class Request6 extends HttpServlet {

public void doget (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Request.getrequestdispatcher ("/header.html"). Include (request, response);

Response.getoutputstream (). Write ("Aaaaaaaaaaaa". GetBytes ());

Request.getrequestdispatcher ("/footer.html"). Include (request, response);

}

public void DoPost (HttpServletRequest request, httpservletresponse response)

Throws Servletexception, IOException {

Doget (request, response);

}

}

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.