前言
本文包含request内置对象的使用、乱码处理的两种方法、使用request.getparamter()方法获取表单提交的数据、采用request对象通过getparameter()方法和getparametervalues()方法获取表单请求数据、使用request内置对象时,注意类型转换、空指针异常。
实验要求1
设计并实现一个用户登录的过程,其中login.jsp页面提供一个表单,用于用户输入相应的用户名和密码进行登录,表单提交至checklogin.jsp页面,checklogin.jsp用于登录验证,检查用户名和密码是否正确,如果用户输入用户名computer,密码jsp后,则使用用<jsp:forward>动作标记跳转到success.jsp页面,否则,跳转到fail页面。
实验代码
login.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8" %>
<html>
<head>
<title>用户登录</title>
</head>
<body>
<br/>
<form action="checklogin.jsp" method="post" target="_blank">
<table border="1" width="500px" align="center">
<th colspan="2">用户登录</th>
<tr>
<td>用户名</td>
<td><input type="text" name="names" /></td>
</tr>
<tr>
<td>密码</td>
<td> <input type="password" name="password" /></td>
</tr>
<tr>
<td><input type="submit" value="提交" /></td>
<td><input type="reset" value="重置" /></td>
</tr>
</table>
</form>
</body>
</html>
checklogin.jsp
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=utf-8" pageencoding="utf-8" %>
<html>
<head></head>
<body>
<%
string user = request.getparameter("names");
string password = request.getparameter("password");
if(user.equals("computer")){
if(password.equals("jsp")){
%>
<jsp:forward page="./success.jsp"></jsp:forward>
<%
}else{
%>
<jsp:forward page="./fail.jsp"></jsp:forward>
<%
}
}else{
%>
<jsp:forward page="./fail.jsp"></jsp:forward>
<%
}
%>
</body>
</html>
success.jsp
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=utf-8" pageencoding="utf-8" %>
<html>
<head>
<title>success</title>
</head>
<body>
<h1>success!</h1>
</body>
</html>
fail.jsp
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=utf-8" pageencoding="utf-8" %>
<html>
<head>
<title>success</title>
</head>
<body>
<h1>fail!</h1>
</body>
</html>
实验截图
实验要求2
编写一个jsp页面input.jsp,该页面提供一个表单,用户通过表单输入两个整数,及四则运算符号,提交表单至count.jsp页面,该页面负责根据选择的运算符计算出结果。
实验代码
input.jsp
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=utf-8" pageencoding="utf-8" %>
<html>
<head>
<title>简单计算器</title>
<style>
body {
background-color: yellow;
}
</style>
</head>
<body>
<form action="count.jsp" method="post">
<h2>输入运算数、选择运算符号:</h2>
<input type="text" name="a" />
<select size='1px' name="b" />
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<input type="text" name="c" />
<br/>
<br/>
<input type="submit" value="运行结算结果" />
</form>
</body>
</html>
count.jsp
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=utf-8" pageencoding="utf-8" %>
<html>
<head>
<title>计算结果</title>
<style>
body {
background-color: yellow;
}
</style>
</head>
<body>
<h2>计算结果:
<%
string stra=request.getparameter("a");
string strb=request.getparameter("b");
string strc=request.getparameter("c");
float fa = float.parsefloat(stra);
float fc = float.parsefloat(strc);
system.out.print(strb);
if(strb.equals("+")){
out.print(fa+strb+fc+"="+(fa+fc));
}else if(strb.equals("-")){
out.print(fa+strb+fc+"="+(fa-fc));
}else if(strb.equals("*")){
out.print(fa+strb+fc+"="+(fa*fc));
}else{
out.print(fa+strb+fc+"="+(fa/fc));
}
%>
</h2>
</body>
</html>
实验截图
实验要求3
乱码问题:编写两个jsp页面,分别是question.jsp和answer.jsp
要求在question.jsp页面里利用表单,提供如下页面,提交表单至answer.jsp页面,在answer.jsp页面实现判断用户回答是否正确。
实验代码
question.jsp
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=utf-8" pageencoding="utf-8" %>
<html>
<head>
<title>问题页面</title>
<style>
body {
background-color: pink;
}
h2 {
color: blue;
}
</style>
</head>
<body>
<form action="answer.jsp" method="post">
<h2>小说围城的作者是:</h2>
<input type="radio" name="a" value="钱钟书">a.钱钟书
<input type="radio" name="a" value="海岩">b.海岩
<input type="radio" name="a" value="路遥">c.路遥
<input type="radio" name="a" value="韩寒">d.韩寒
<br>
<h2>你意愿的工作城市:</h2>
<input type="checkbox" name="b" value="北京">a.北京
<input type="checkbox" name="b" value="天津">b.天津
<input type="checkbox" name="b" value="上海">c.上海
<input type="checkbox" name="b" value="黄骅">d.黄骅
<br>
<h2>请输入姓名:</h2>
<input type="text" name="name">
<input type="submit" value="提交验证">
</form>
</body>
</html>
answer.jsp
<%@page import="javax.servlet.annotation.handlestypes"%>
<%@page import="java.util.enumeration"%>
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=utf-8" pageencoding="utf-8" %>
<html>
<head>
<title>回答结果</title>
<style>
body {
background-color: #90bbde;
}
</style>
</head>
<body>
<h2>
<%
string str = request.getparameter("a");
string strtemp = new string(str.getbytes("iso-8859-1"),"utf-8");
system.out.print(strtemp);
string temp = new string("钱钟书".getbytes("iso-8859-1"),"utf-8");
if(strtemp.equals("钱钟书")){
string name1 =request.getparameter("name");
string nametemp = new string(name1.getbytes("iso-8859-1"),"utf-8");
%>
恭喜你,
<%= nametemp %>
回答正确,加两分!
<%
}else{
%>
很遗憾,回答错误!
<%
}
string[] strb=request.getparametervalues("b");
%>
<br> 你意愿的工作有
<%= strb.length %>个,分别是:
<%
for(int i=0;i<strb.length;i++){
string strbtemp = new string(strb[i].getbytes("iso-8859-1"),"utf-8");
out.print(" "+strbtemp);
}
%>
</h2>
</body>
</html>
实验截图
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!