大学jsp实验5request,response

时间:2021-04-29 21:08:26

1、request对象的使用

(1)编写一个包含有表单的JSP页面form.jsp,其中包含可以输入姓名和出生地的文本框,提交表单后在另一个页面中显示用户提交的姓名和出生地。请写出相应代码:

form.jsp:

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>form</title>
</head>
<body>
<form action="display.jsp"method="post" name="name">
8 姓名:<input type="text" name="username"> 出生地:<input type="text" name="from"> <input type="submit" name="submit" value="提交"> </form> </body> </html>

display.jsp:

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>display</title>
</head>
<body>
<% out.print("姓名:"+request.getParameter("from"));
out.print("<br>");
out.print("出生地:"+request.getParameter("username"));
%>
</body>
</html>

(2)编写一个名为shiyan5_2form.jsp的JSP文件,浏览效果如下图所示:

大学jsp实验5request,response

再编写一个名为shiyan5_2display.jsp的JSP文件,用来显示用户提交的信息,浏览效果如下图所示:

大学jsp实验5request,response

请编写相应代码:(注意中文乱码的处理):

shiyan5_2form.jsp:

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test2_1</title>
</head>
<body>
<p>请输入下列信息</p>
<form action="shiyan5_2display.jsp" method="post" name="name">
输入您的姓名:<input type="text" name="username"><br>
请选择您的性别:<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女<br>
您喜欢的歌手:<input type="checkbox" name="singer" value="张学友">张学友
<input type="checkbox" name="singer" value="李健">李健
<input type="checkbox" name="singer" value="刘德华">刘德华
<input type="checkbox" name="singer" value="王菲">王菲<br>
<input type="hidden" name="hide" value="这是隐藏信息">
<input type="submit" value="提交">
</form>
</body>
</html>

shiyan5_2display.jsp:

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test2_1</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8"); out.print(request.getParameter("username"));
out.print("<br>" + request.getParameter("sex") + "<br>");
String[] singer = request.getParameterValues("singer");
if (singer == null) {
out.print("请输入相关信息");
} else {
for (String s : singer) {
out.print(s + " ");
}
}
out.print("<br>" + request.getParameter("hide"));
%>
</body>
</html>

(3)编写一个shiyan5_3_a.jsp文件,浏览效果如下图所示:

大学jsp实验5request,response

单击“确认”超链接后,显示如下shiyan5_3_b.jsp页面:

大学jsp实验5request,response

请写出相应代码:

shiyan5_3_a.jsp:

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test2_1</title>
</head>
<body>
<p>信息确认:</p>
<hr>
<%
String name = "张三";
String banji = "2015级1班";
String project = "跳远";
out.print("姓名:"+name+",班级:"+banji+",申报项目:"+project);
%><br>
如信息无误,请点击<a href="shiyan5_3_b.jsp?name=张三&banji=2015级1班&project=跳远">确认</a>
</form>
</body>
</html>

shiyan5_3_b.jsp:

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>test2_1</title>
</head>
<body>
<p>您的信息如下:</p>
<hr>
<%
request.setCharacterEncoding("utf-8");
out.print("姓名:"+request.getParameter("name")+"<br>");
out.print("班级:"+request.getParameter("banji")+"<br>");
out.print("项目:"+request.getParameter("project"));
%>
</body>
</html>

2、response对象的使用

(1)下面的JSP页面,如果请求该页面时想让页面以word方式打开,请在横线上填写正确的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body bgcolor=cyan style="font-size:28px">

<p>学习response对象的setContentType方法</p>

</body>

</html>

 <% response.setContentType("application/msword;charset=UTF-8"); %>

(2)编写如下登录页面shiyan5_4.jsp:

大学jsp实验5request,response

在其中输入用户名和密码,如用户名和密码无误(正确的用户名和密码自定,如用户名为张三,密码为123456)则显示欢迎页面shiyan5_4_receive.jsp,如下图所示:

大学jsp实验5request,response

若用户或密码错误,则仍然显示登录页面,等待用户输入用户名和密码。此外,如用户直接请求shiyan5_4_receive.jsp,仍会显示登录页面。

请写出相应代码:

shiyan5_4.jsp:
 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<p style="text-align: center;">系统登陆</p>
<hr>
<form action="shiyan5_4_receive.jsp" method="post" name="login" style="text-align: center">
用户名:<input type="text" name="username"><br><br>
密 码:<input type="password" name="password"><br><br><br>
<input type="submit" name="submit" value="确定">
<input type="reset" name="reset" value="取消">
</form>
</body>
</html>
shiyan5_4_recevie.jsp:
 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String username = "";
String password = "";
username = request.getParameter("username");
password = request.getParameter("password");
if (username==null || password==null) {
response.sendRedirect("shiyan5_4.jsp");
}else {
if (!username.equals("张三") || !password.equals("123456")) {
response.sendRedirect("shiyan5_4.jsp");
}
} %>
登陆成功,用户<%=username%>,你好!
</body>
</html>