Rookie Learn Android Note (32): Request for information on client

Source: Internet
Author: User

I. The concept and structure of request

1. Concept

The request object is to make requests from the client to the server, including information submitted by the user and some information from the client. The client submits the data through an HTML form or a method that provides parameters after the page's address, and then obtains the data through the related methods of the request object. The various methods of request are primarily used to handle the parameters and options in requests submitted by the client browser

2. Structure

Request
ServletRequest-General request, which provides the most basic method that a request should have
|
|--httpservletrequest-ServletRequest's child, further enhancements to the HTTP protocol

Ii. Requset Obtaining Client Information
Common features:
The Getrequesturl method returns the client to make a request for the full URL
!! 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
!! GetMethod How to get the client request
!! Getcontextpath get the current Web App virtual directory name

Example:

Package Com.java.request;import Java.io.ioexception;import Javax.servlet.genericservlet;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class CustInfoServlet Extends Httpservlet{public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//1. Gets the full urlstring URL of the client request = Request.getrequesturl (). toString (); SYSTEM.OUT.PRINTLN ("Client-requested full URL:" +url);//2. Gets the name of the resource portion of the client request string URI = Request.getrequesturi (); System.out.println ("Name of the resource part of the client request:" +uri);//3. Gets the parameter part of the request line string qstr = Request.getquerystring (); SYSTEM.OUT.PRINTLN ("parameter in Request line:" +QSTR);//4. Gets the IP address of the requesting client string IP = request.getremoteaddr (); SYSTEM.OUT.PRINTLN ("IP address of the client:" +ip);//5. Gets the client's request in the form of string method = Request.getmethod (); SYSTEM.OUT.PRINTLN ("Client request mode:" +method);//6. Gets the name of the current web App string name = Request.getcontextpath (); System.out.println ("Name of the Web app:" +name);//response.sendredirect (reQuest.getcontextpath () + "/index.jsp");} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}
Note: In the future, the Web App directory name is used Getcontextpath, and note that the name of the Web App catalog and the project are two different concepts, how to modify the Web App directory?

Modified in Project->properties->myeclipse->web directory, re-published after modification



Rookie Learn Android Note (32): Request for information on client

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.