A good memory is better than a bad pen 6-httpservletrequest do Web request forwarding

Source: Internet
Author: User
Tags get ip


in many web scenarios, we need to forward requests.

For example some system upgrade, but the old user may not have the condition to upgrade, then still go the original way, but we do not need to support a variety of platforms, and finally through the internal forwarding to the new function module.

Request Forwarding: When a Web resource receives a client request, notifies the server to invoke another Web resource for processing.

1 , How to implement request forwarding in a Servlet

1 , through the ServletContext Getrequestdispatcher (Stringpath) method, the method returns a RequestDispatcher object, Call this object's forward method to implement request forwarding.

Example: The change.jsp page that forwarded the request

RequestDispatcher Reqdispatcher=this.getservletcontext (). Getrequestdispatcher ("/change.jsp");

Reqdispatcher.forward (Request,response);

2 , through the Getrequestdispatche (Stringpath) method provided by the request object, the method returns a RequestDispatcher object that calls the forward method of the object to enable request forwarding.

Example: The change.jsp page that forwarded the request

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

This method is generally used.

2 , implementing the Java code for request forwarding

Package com.servlet;

Import java.io.IOException;

Import javax.servlet.ServletException;

Import Javax.servlet.http.HttpServlet;

Importjavax.servlet.http.HttpServletRequest;

Importjavax.servlet.http.HttpServletResponse;

/**

 * Get the client request information through the request object and forward it to another page

*

* @author Fan Fangming

*/

public class RequestDispatcher Extendshttpservlet {

Privatestatic final Long serialversionuid = -4150164731865037680l;

Publicvoid doget (httpservletrequest request, httpservletresponse response)

Throwsservletexception, IOException {

Stringrealip = This.getrealip (request);

Request.setattribute ("Realip", Realip);

        // request forwarded to change.jsp

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

}

Publicvoid DoPost (httpservletrequest request, httpservletresponse response)

Throwsservletexception, IOException {

Doget (Request,response);

}

    // If the IP address is changed by proxy, etc.

Publicstring Getrealip (HttpServletRequest request) {

STRINGIP = Request.getheader ("X-forwarded-for");

if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {

ip= Request.getheader ("Proxy-client-ip");

}

if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {

ip= Request.getheader ("Wl-proxy-client-ip");

}

if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {

ip= request.getremoteaddr ();

}

Returnip;

}

}

3 , change.jsp source code

<%@ page language= "java" import= "java.util.*,javax.servlet.http.*" pageencoding= "UTF-8"%>

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

<title> Request Forwarding </title>

<body>

Get the caller's IP address:

your IP address is: <%= (String) request.getattribute ("Realip")%>

</body>

4 , Web. xml File

<?xml version= "1.0" encoding= "UTF-8"?>

<web-app version= "3.0"

Xmlns= "Http://java.sun.com/xml/ns/javaee"

Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"

Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee

Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">

<display-name></display-name>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<servlet>

<servlet-name>request</servlet-name>

<servlet-class>com.servlet.Request</servlet-class>

</servlet>

<servlet>

<servlet-name>change</servlet-name>

<servlet-class>com.servlet.RequestDispatcher</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>change</servlet-name>

<url-pattern>/change</url-pattern>

</servlet-mapping>

</web-app>

5 , Java get IP Address: request.getremoteaddr () problem

The method for obtaining the IP address of the client is: Request.getremoteaddr (), which is valid in most cases. However, the real IP address of the client cannot be obtained through the reverse proxy software such as Apache,squid.

If the reverse proxy software is used, the IP address obtained by using the Request.getremoteaddr () method is the address of the reverse proxy software, not the real IP of the client.

After the agent, due to the addition of the middle tier between the client and the service, so the server can not directly get the client's IP, the server-side application can not directly forward the requested address to the client. However, the x-forwarded-for information is added to the HTTP header information of the forwarding request. Used to track the original client IP address and the server address of the original client request.

So in the Java source code that implements the request forwarding ,

    // If the IP address is changed by proxy, etc.

Publicstring Getrealip (HttpServletRequest request) {

STRINGIP = Request.getheader ("X-forwarded-for");

if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {

ip= Request.getheader ("Proxy-client-ip");

}

if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {

ip= Request.getheader ("Wl-proxy-client-ip");

}

if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {

ip= request.getremoteaddr ();

}

Returnip;

}

Is the method of acquiring the real IP , but this method cannot handle the address that was hacked and modified.

A good memory is better than a bad pen 6-httpservletrequest do Web request forwarding

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.