Session-Tracking Technology example--hiding form fields

Source: Internet
Author: User
Tags http request

In my last article, I've introduced several ways to implement session tracking Http://blog.csdn.net/lvpin/archive/2007/06/09/1645778.aspx

Now use a different code example to illustrate the implementation of session tracking--hiding form fields.

Examples are as follows: The first page is the landing page, the second page does nothing, only responsible for submitting to the third page, the third page is responsible for displaying the user name from the first page. 3 pages, 2 different requests, but you can also implement session tracking.

login.jsp

<%@ page contenttype= "text/html; CHARSET=GBK "%>
<title>
Landing page
</title>
<body bgcolor= "#536da5" >
Landing page
<form method= "POST" action= "/hiddenmodule/hiddenservlet" >-page Submit post method, processed by the Hiddenservlet form
<br><br>
<table width= "100%" align= "Center" >
<tr>
<td> name </td>
<td><input type= "text" name= "UserName"/></td>--Name
</tr><tr>
<td> Password </td>
<td><input type= "Password" name= "Userpwd"/></td>--Password
</tr><tr>
<td><input type= "Submit" name= "Submit" value= "Landing" ></td>
<td><input type= "reset" value= "reset" ></td>
</tr>
</table>
</form>
</body>

Hiddenservlet.java

Package Hiddendemo;

Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
Import java.util.*;

public class Hiddenservlet
Extends HttpServlet {

public void DoPost (HttpServletRequest request, httpservletresponse response) throws
Servletexception, IOException {
The Doget () of the System.out.println ("Hiddenservlet") has been invoked. ");
Response.setcontenttype ("text/html; CHARSET=GBK ");
PrintWriter out = Response.getwriter ();
String userName = Request.getparameter ("UserName"); Gets the value from the request page called the username parameter
Out.println ("Out.println ("

Back to the client page, if this page is submitted, the Post method processing the request, the page submitted by Infoservlet to deal with
Out.println ("<form action=/"/hiddenmodule/infoservlet/"method=/" post/">");
Out.println ("<body bgcolor=/" #ffffff/">");
Out.println ("

Out.println ("<table width=/" 100%/"align=/" center/">");
Out.println ("<tr><td>");
Out.println ("<input type=/" hidden/"name=/" hiddenname/"value=/"); Hide form fields
Out.println (UserName); Hidden value, is the user name from the first page
Out.println ("/");
Out.println ("<p> has placed the name from the first page in a hidden field, transparent to the user." ");
Out.println ("</td></tr><tr><td>");
Out.println ("<input type=/" submit/"value=/" to the next page to see/">");
Out.println ("</td></tr></table></body>Out.close ();
}
}

Infoservlet.java

Package Hiddendemo;

Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
Import java.util.*;

public class Infoservlet
Extends HttpServlet {

public void DoPost (HttpServletRequest request, httpservletresponse response) throws
Servletexception, IOException {
Response.setcontenttype ("text/html; charset=gb2312 ");
PrintWriter out = Response.getwriter ();
String userName = Request.getparameter ("Hiddenname"); --The value from the second page is invisible on the second page
Out.println ("Out.println ("<body bgcolor=/" #ffffff/">");
Out.println ("<p> User name is:" + userName +--Print out
". This is the value from the hidden field submission from the Hiddenservlet postback page .</p> ");
Out.println ("</body>Out.close ();
}
}

login.jsp

LOGIN.JSP Submit to Hiddenservlet processing

by Hiddenservelt through OUT.PRINTLN () to generate a client postback on the page as above, click "Turn to next page to see" When the Out.println ("<form action=/"/hiddenmodule/  infoservlet/"method=/" post/">"); Inside the Infoservlet processing, the results are as follows:

··················································

This example displays the value submitted from the first page in the third page by hiding the form field, and hides it in the second page, and the third page can get the result when the form is submitted.

But the inside can not handle Chinese, if the first page input is Chinese, then in the second page, and the third page is garbled. This is because the data for the form is stored in the request object. The requested data is transmitted over the network in the form of a byte stream. When the server receives an HTTP request in the form of a byte stream, it parses the data in a is0-8859-1 manner. This will not display the Chinese data in the form correctly if the output page sets the character set to GB2312/GBK.

about how to correctly deal with Chinese garbled article in the next time to write a detailed.

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.