Forwards. Hides the Jsp.url address. MD

Source: Internet
Author: User

First, forwarding parameters:

1. Transfer the parameters inside the JSP via Loginservlet to Pageselvert:

@WebServlet("/login") Public  class loginservlet extends httpservlet { protected void dopost(httpservletrequest request,httpservletresponse response)  throws servletexception,ioexception{//forward to PageservletRequest.getrequestdispatcher ("/page"), ForWord (REQUEST,REAPOMSE); }}@WebServlet("page") Public  class pageservlet extends httpservlet { protected void dopost(httpservletrequest request,httpservletresponse response)  throws servletexception,ioexception{String usename = Request.getparameter ("Usename"); String Password = request.getparameter ("password"); System.out.pritln (Usename); System.out.pritln (PASSWORRD);}}

2. Bring the values stored in the Loginservlet to the Pageservlet:

@WebServlet("/login") Public class loginservlet extends httpservlet {Protected void dopost(httpservletrequest request,httpservletresponse response) {//In the Request object, set the property value, as long as it is the same request object to obtain this data (so forward can, redirect No, redirect is two) //Send e-mail to the past (only in front, if it is after the Getrequestdispatcher (behind), you will not get it)Request.setattribute ("Email", "123456@163. com "); Request.getrequestdispatcher ("/page"), ForWord (Request,response);}}@WebServlet("/page") Public class pageservlet extends httpservlet {Protected void dopost(httpservletrequest request,httpservletresponse response) {//Get [email protected]string email = (string) request.getattribute ("email");//Delete email valuesRequest.removeattribute ("email");//Get all the namesRequest.getattributenames (); String usename = Request.getparameter ("Usename"); String Password = request.getparameter ("password");}}

* * Forwarding Parameters: * * removeattribute Delete getattributenames get all the names SetAttribute set value getattribute get value

The request response their life cycle, which ends between requests and responses.

Second, hidden JSP:

You can put the JSP into the Web-inf directory, and later can only use forwarding to access the following JSP

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

Purpose: Hide the JSP to change the access page to the following:

<welcome-file-list><welcome-file>/web-inf/pages/index.jsp</ welcome-file></welcome-file-list>

Front add "/" directly to the place where it is the root directory

//admin for a virtual clip @WebServlet("/admin/test") Public  class testservlet entends httpservlet { protected void deget(httpservletquest req,httpservletresponse resp) throws Servletexception,ioeception{Resp.sendredirect ("index.jsp");}}

There are 3 ways to access it:

//"..." Represents jumping up a directory in the index.jsp directory resp.sendredirect (".. /index.jsp ");
System.out.println (Req.getcontextpath ()); //servlet7_url Resp.sendredirect (Req.getcontextpath () +"/index.jsp");
"/");
Third, garbled problem:

TOMCAT7 version conversion garbled need to see the method to go to get the String class transcoding post directly set the code can be

String s=req.getparament ("text");        ↓
<fromactoin="lm"method="Get">
System.out.println (new String (s.getbytes (iso-8859),"Utf-8")), req, Setcharacterencoding ("UTF-8"); String s=req.getparamenter ("test");        ↓
<fromactoin="lm"method="POST">
System.out.println (s);

TOMCAT8 version does not require a half-paragraph method, directly set the transcoding can be

Req.setcharacterencoding ("UTF-8"); String s=req.getparameter ("test"); System.out.println (s);

If you do not transcode, direct printing, there will be garbled, such as:

If you want to nginx the word inside a servlet into another servlet, you need to transcode it, such as:

Request.sendredirect (text1?name= "dog Son");

This should be written as:

 Text0Servlet:Request.sendRedirect ("Text1?name=" +urlencode.encode ("dog Son")); Text1servlet: //tomcat8  Request.setcharacterencoding ("Utf-8 "); //tomcat 7  String S=new  String (request.getparameter ("name"). GetBytes ("Iso-8859 - 1  ")," Utf-8  "); System.out.println (Request.getparameter ("name")); 
Resp.sendredirect ("test1?name=" +urlencoder.encode ("polymorphic","UTF-8"));                           ↓req.setcharacterencoding ("UTF-8"//string s=new String (req.getparameter ("name"). GetBytes (" Iso-8859-1 ")," UTF-8 ") System.out.println (Req.getparameter);

Forwards. Hides the Jsp.url address. MD

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.