下载并安装了JDK,TOMCAT,IDEA![](https://i-blog.csdnimg.cn/blog_migrate/b72a73b39fa5716bbd033842cf06b770.png)
学会用记事本编写jsp文件,并放进tomcat的相关目录下,运行。在IDEA编辑器中新建java web项目,并配置tomcat,运行jsp页面。
JSP编程练习:
(1)用JSP实现用户获取用户输入的操作。(仿教材例5-5)
input.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<title>input</title>
</head>
<body>
<form action="output.jsp" method="post">
<table border="1px solid" frame="void">
<tr>
<td>
姓名:
</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>
性别:
</td>
<td>
<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女
</td>
</tr>
<tr>
<td>
爱好:
</td>
<td>
<div>
<input type="checkbox" name="like" value="篮球">篮球
<input type="checkbox" name="like" value="旅游">旅游
<input type="checkbox" name="like" value="音乐">音乐
<input type="checkbox" name="like" value="摄影">摄影
</div>
</td>
</tr>
<tr>
<td>
E-mail:
</td>
<td>
<input type="text" name="E-mail">
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type="submit" value="Submit">
<input type="reset" value="全部重写">
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>output</title>
</head>
<body>
<table border="1px solid" frame="void">
<caption>用户输入的信息</caption>
<tr>
<td>
姓名:
</td>
<td>
<%=new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8")%>
</td>
</tr>
<tr>
<td>
性别:
</td>
<td>
<%=new String(request.getParameter("sex").getBytes("ISO-8859-1"),"UTF-8")%>
</td>
</tr>
<tr>
<td>
爱好:
</td>
<td>
<%
String []like= request.getParameterValues("like");
for(int i=0;i< like.length;i++){
%>
<%=new String(like[i].getBytes("ISO-8859-1"),"UTF-8")+" "%>
<%
}
%>
</td>
</tr>
<tr>
<td>
E-mail:
</td>
<td>
<%=new String(request.getParameter("E-mail").getBytes("ISO-8859-1"),"UTF-8")%>
</td>
</tr>
</table>
</body>
</html>
(2)用JSP实现用户登录验证的功能。
如果用户输入正确的账号密码,则提示问候语句;如果用户输入错误的账号密码,则提示账号密码有误。
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<form action="LoginServlet" method="post">
<table align="center">
<tr>
<td>账号:</td>
<td><input name="name"type="text"></td>
</tr>
<tr>
<td>密码:</td>
<td><input name="password"type="text"></td>
</tr>
<tr>
<td align="center"colspan="1">
<input type="submit"value="登陆">
</td>
</tr>
</table>
</form>
</body>
</html>
LoginServlet.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
String name = request.getParameter("name");
String password = request.getParameter("password");
if(name!=null){
if(name.equals("1603633747")&&password.equals("123456")) {
out.println("您好,周哈哈!");
}
else{
out.println("您输入的账号密码有误,请重新输入!");
}
}
else{
out.println("请输入账号!");
}
%>
</body>
</html>
任意编写3-4个页面,为每个页面添加权限控制。如果用户已经登录,则在每个页面页首提示:欢迎你,**。如果没有登录跳转到这个页面,则提示:非法访问。
login1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>登录</title>
</head>
<body>
<h2>登录</h2>
<form action="loginCheck.jsp" method="post">
用户名:<input type="text" name="username" required><br>
密码:<input type="password" name="password" required><br>
<input type="submit" value="登录">
</form>
</body>
</html>
loginCheck.jsp
<%@ page import="java.util.*" contentType="text/html;charset=UTF-8" language="java" %>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
// 简单的用户名和密码验证,实际中应从数据库验证
if ("Zhou".equals(username) && "123456".equals(password)) {
session.setAttribute("username", username); // 存储用户名到session中
response.sendRedirect("welcome.jsp"); // 登录成功,跳转到欢迎页面
} else {
response.sendRedirect("login1.jsp?error=1"); // 登录失败,重定向回登录页面并传递错误参数
}
%>
welcome.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>欢迎页面</title>
</head>
<body>
<%
String username = (String) session.getAttribute("username");
if (username != null) {
out.println("<h2>欢迎你," + username + "!</h2>");
} else {
out.println("<h2>非法访问!</h2>");
}
%>
</body>
</html>