JSP built-in Object---Request object (User login page (return value and array: gerparameter,getparametervalues))

Source: Internet
Author: User
Tags tomcat server

Create two JSP pages: Reg.jsp and request.jsp

REG.JSP:

<%@ page language= "java" import= "java.util.*" contenttype= "text/html"; Charset=utf-8 "%>

<%@ page import= "java.text.*"%>
<%
String path = Request.getcontextpath ();
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>insert title here</title>
<body>
<form action= "request.jsp" name= "LoginForm" method= "POST" >

<table>
<tr>
<td> User name:</td>
<td><input type= "text" name= "username"/></td>
</tr>


<tr>
<td> Hobby:</td>
<td>
<input type= "checkbox" Name= "favorite" value= "read"/> Reading
<input type= "checkbox" Name= "Favorite" value= "Yujia"/> Yoga
<input type= "checkbox" Name= "Favorite" value= "Fadai"/> Daze
</td>
</tr>


<tr>
<td> Show:</td>
<TD colspan= "2" ><input type= "Submit" name= "Commit"/></td>
</tr>
</table>
</form>
</body>

REQUEST.JSP:

<%@ page language= "java" import= "java.util.*" contenttype= "text/html; charset=utf-8"
%>
<% @page Import= "java.text.*"%>
<%
String path = Request.getcontextpath ();
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd";
<meta http-equiv= "Content-type" content= "text/html; charset=utf-8";
<title >insert title here</title>
<body>
< %
Request.setcharacterencoding ("Utf-8");//solve Chinese garbled problem
%>
<!--username Returns a value, using Gerparameter,  Hobby returns is an array of strings, with getparametervalues-->
User name: <%=request.getparameter ("username")%>
Hobbies: <%
string[] Favorites = request.getparametervalues ("favorite");
for (int i=0;i<favorites.length;i++)
{
Out.println (favorites[i]+ "&nbsp;&nbsp;");
}
%>
</body>

Operation Result:

A little bit of a mistake in the middle:

(1) The Tomcat project was closed by me, so start the error, open Project can

(2) The reg.jsp <form action= "request.jsp" name= "LoginForm" method= "post" > Added a </form> so the following code is invalid, Mad Dot Submit Button did not respond, and finally with Firefox debugging found, continue to refuel.

Add a URL address to the reg.jsp to see the link

<body>
<form action= "request.jsp" name= "LoginForm" method= "POST" >
<table>
<tr>
<td> User name:</td>
<td><input type= "text" name= "username"/></td>
</tr>
<tr>
<td> Hobby:</td>
<td>
<input type= "checkbox" Name= "favorite" value= "read"/> Reading
<input type= "checkbox" Name= "Favorite" value= "Yujia"/> Yoga
<input type= "checkbox" Name= "Favorite" value= "Fadai"/> Daze
</td>
</tr>
<tr>
<td> Show:</td>
<TD colspan= "2" ><input type= "Submit" name= "Commit"/></td>
</tr>
</table>
</form>
<a href= "Request.jsp?username= Zhang Zhang" > test Zhang Zhang Account </a> <!--add URL --
</body>

When the username is in English normal display, when it is Chinese, display garbled, so you can get the script in request.jsp (below), unable to resolve the URL to pass the Chinese garbled problem

<%
Request.setcharacterencoding ("Utf-8");//solve Chinese garbled problem
%>

Resolved as follows:

Modify the Tomcat configuration file Server.xml, address: D:\Program files\eclipse\apache-tomcat-7.0.69\conf, add properties: uriecoding= "Utf-8", Reboot the Tomcat server to take effect

In addition, because add Url,url only user name, no hobby, pass the URL parameter only user name, no hobby, so in get hobby times null pointer exception, so in do traverse output hobby to add judgment (not equal to empty continue to execute code, equal to empty does not output), request.jsp output Hobby Part code changes:

Hobbies: <%
if (Request.getparametervalues ("favorite")!=null) {//Plus judgment
string[] Favorites = request.getparametervalues ("favorite");
for (int i=0;i<favorites.length;i++)
{
Out.println (favorites[i]+ "&nbsp;&nbsp;");
}
}

JSP built-in Object---Request object (User login page (return value and array: gerparameter,getparametervalues))

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.