JSP之request对象
原创
©著作权归作者所有:来自51CTO博客作者mb6434c781b2176的原创作品,请联系作者获取转载授权,否则将追究法律责任
接收表单
index.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="common.jsp">
请输入:<INPUT TYPE="text" NAME="name"><INPUT TYPE="submit" VALUE="提交">
</FORM>
</BODY>
</HTML>
|
common.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<%
request.setCharacterEncoding("gb2312");
String name = request.getParameter("name");
%>
您输入的是:<FONT COLOR="RED"><%=name%></FONT>
</BODY>
</HTML>
|
文本框和密码框都是将控件里的数据作为字符串传入服务器,某些时候需要转换其类型,例如:
index.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<H3>计算两数之和</H3>
<FORM METHOD=POST ACTION="common.jsp">
数1:<INPUT TYPE="text" NAME="num1"><BR>
数2:<INPUT TYPE="text" NAME="num2"><BR>
<INPUT TYPE="submit" VALUE="加法">
</FORM>
</BODY>
</HTML>
|
common.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<%
request.setCharacterEncoding("gb2312");
String num1 = request.getParameter("num1");
String num2 = request.getParameter("num2");
int i = 0 ;
int j = 0 ;
try
{
i = Integer.parseInt(num1) ;
j = Integer.parseInt(num2) ;
%>
两数之和是:<FONT COLOR="RED"><%=(i+j)%></FONT>
<%
}
catch(Exception e)
{
%>
Input Error!!!
<%
}
%>
</BODY>
</HTML>
|
如果传递的参数有中文字符,则需要用request.setCharacterEncoding("gb2312");用以消除中文显示乱码隐患。
我们再看看表单中其它比较复杂的控件。
index.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<H3>请选择</H3>
<FORM METHOD=POST ACTION="common.jsp">
<INPUT TYPE="checkbox" NAME="like" VALUE="篮球">篮球
<INPUT TYPE="checkbox" NAME="like" VALUE="足球">足球
<INPUT TYPE="checkbox" NAME="like" VALUE="游泳">游泳
<INPUT TYPE="checkbox" NAME="like" VALUE="击剑">击剑
<INPUT TYPE="checkbox" NAME="like" VALUE="摔跤">摔跤<BR>
<INPUT TYPE="submit" VALUE="提交">
</FORM>
</BODY>
</HTML>
|
common.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<%
request.setCharacterEncoding("gb2312") ;
String like[] = request.getParameterValues("like") ;
%>
<H2>兴趣为:</H2>
<%
if(like!=null){
for(int i=0;i<like.length;i++)
{
%>
<%=like[i]%>、
<%
}
}
%>
</BODY>
</HTML>
|
上例给出了接收复选框数据的方法,但是要注意当一个都不选时的空指针异常的出现。
request.getParameter()方法还可以接收隐藏表单和URL后面传过来的参数,看下面的例子:
index.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="common.jsp">
密码:<INPUT TYPE="password" NAME="password">
<INPUT TYPE="hidden" NAME="name" VALUE="aaa"><BR>
<INPUT TYPE="submit" VALUE="提交"><BR>
<A HREF="common.jsp?password=111&name=aaa">提交</A>
</FORM>
</BODY>
</HTML>
|
common.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<%
request.setCharacterEncoding("gb2312") ;
String password = request.getParameter("password") ;
String name = request.getParameter("name") ;
%>
<h1>内容为:<%=password%></h1>
<h1>内容为:<%=name%></h1>
</BODY>
</HTML>
|
最后,我们看一个下拉列表的例子:
index.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST ACTION="common.jsp">
<SELECT NAME="city">
<OPTION VALUE="武汉">武汉</OPTION>
<OPTION VALUE="上海">上海</OPTION>
<OPTION VALUE="北京">北京</OPTION>
<OPTION VALUE="天津">天津</OPTION>
<OPTION VALUE="重庆">重庆</OPTION>
</SELECT>
<INPUT TYPE="submit" VALUE="提交">
</FORM>
</BODY>
</HTML>
|
common.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<%
request.setCharacterEncoding("gb2312") ;
String city = request.getParameter("city") ;
%>
<h1>城市:<%=city%></h1>
</BODY>
</HTML>
|
保存及获取属性
index.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<%
request.setAttribute("name","aaa") ;
request.setAttribute("password","111") ;
%>
<jsp:forward page="common.jsp"/>
</BODY>
</HTML>
|
common.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>request对象</TITLE>
</HEAD>
<BODY>
<H2>这里是common.jsp</H2>
<%
String name = (String)request.getAttribute("name") ;
String password = (String)request.getAttribute("password") ;
%>
<h1>name : <%=name%></h1>
<h1>password : <%=password%></h1>
</BODY>
</HTML>
|
这个例子会成功的取得保存在request对象里的属性值。如果我们将<jsp:forward page="common.jsp"/>去掉,改由response.sendRedirect("common.jsp");来跳转,或者由<A HREF="common.jsp">转向</A>这个超链接来转向,都是无法取得request对象中的属性值的。
登录范例
index.jsp:
<%@page contentType="text/html;charset=gb2312"%> <HTML> <HEAD> <TITLE>登录范例</TITLE> </HEAD> <BODY> <FORM METHOD=POST ACTION="login.jsp"> 帐号:<INPUT TYPE="name" NAME="name"><BR> 密码:<INPUT TYPE="password" NAME="password"><BR> <INPUT TYPE="submit" VALUE="登录"> </FORM> </BODY> </HTML> |
login.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>登录范例</TITLE>
</HEAD>
<BODY>
<%
request.setCharacterEncoding("gb2312");
String name = request.getParameter("name");
String password = request.getParameter("password");
if("aaa".equals(name)&&"111".equals(password))
{
%>
<jsp:forward page="success.jsp"/>
<%
}
else
{
%>
<jsp:forward page="failure.jsp"/>
<%
}
%>
</BODY>
</HTML>
|
success.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>登录成功</TITLE>
</HEAD>
<BODY>
<H2>欢迎<%=request.getParameter("name")%>登录</H2>
</BODY>
</HTML>
|
failure.jsp:
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>登录失败</TITLE>
</HEAD>
<BODY>
<H2>登录失败</H2>
</BODY>
</HTML>
|
获得客户端IP
<%@page contentType="text/html;charset=gb2312"%>
<HTML>
<HEAD>
<TITLE>登录范例</TITLE>
</HEAD>
<BODY>
您的IP地址:<FONT COLOR="RED"><%=request.getRemoteAddr()%></FONT>
</BODY>
</HTML>
|