MySQL表结构
MySQL数据生成[Python]
然后复制结果扔SQL里面执行,懒得整python的链接数据库直接插数据
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Author:Mondayice
# @Date 2021年06月14日
#
#insert into users(username,email,password) Values(?,?,?);
for i in range(6,26):
print("insert into users(username,email,password) Values(\'test{}\',\'test{}@lyu.edu.cn\',\'lyuedu{}\');".format(i,i,i))
101九九乘法表
完成一个jsp页面,在页面上以表格的形式输出九九乘法表。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*,java.lang.String" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>101</title>
</head>
<body>
<%
int c = 1;
out.println("<table align='center' border='6'>");
out.println("<caption><h1>九九乘法表</h1></caption>");
//out.println("<tr><th>id</th><th>username</th><th>email</th><th>password</th><th>详细</th></tr>");
for(int a = 1;a<=9;a++){
out.print("<tr>");
for(int b = 1;b<=a;b++){
c = a*b;
out.print("<td>"+b+"*"+a+"="+c+"</td>");
}
out.print("</tr>");
}
%>
</body>
</html>
102早、中、晚好
利用Date类的函数读取系统当前时间,根据不同的时间段,在浏览器输出不同的问候语,例如上午0-12点之间输出“早上好”,下午12-18点输出“下午好”,19-24点输出“晚上好”。
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*,java.lang.String" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>102</title>
</head>
<body>
<%
Date date = new Date();
Calendar c = Calendar.getInstance();
@SuppressWarnings("deprecation")
int hour = date.getHours();
if (hour >= 0 && hour < 12) {
out.println("早上好!");
%>
<br />
<%
} else if (hour >= 12 && hour < 19) {
out.println("下午好!");
%>
<br />
<%
} else {
out.println("晚上好!");
}
%>
<br />
</body>
</html>
103 include 计算
加载动态文件,制作一个JSP文件a,计算给定一个数的平方,然后再制作一个JSP文件b,应用jsp:include动作插入上述的JSP文件a,并且要求向所包含页面a传递参数,指定需要计算的数。
a.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*,java.lang.String" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
int num = Integer.parseInt(request.getParameter("num"));
out.println(num+"的平方是:"+num*num);
%>
</body>
</html>
b.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*,java.lang.String" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<jsp:include page="103a.jsp">
<jsp:param value="19" name="num"/>
</jsp:include>
</body>
</html>
104 四个判断跳转
页面跳转,制作一个jsp页面a,页面上要求有一个表单,表单包含两个单选按钮和一个提交按钮,表单提交到页面b,在b页面中根据用户选择的单选按钮分别用“jsp:forward”语句将页面跳转到页面c或页面d。
a.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*,java.lang.String" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="104b.jsp" method="post">
<input type="radio" name="a" value="male" >男
<input type="radio" name="a" value="female" >女
<input type="submit" value="登录">
</form>
</body>
</html>
b.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*,java.lang.String" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%String b = request.getParameter("a");%>
<%if(b.equals("male")){%>
<jsp:include page="104c.jsp"/>
<% }else if(b.equals("female")){%>
<jsp:include page="104d.jsp"/>
<% } %>
</body>
</html>
c.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*,java.lang.String" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
male 男性
</body>
</html>
d.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*,java.lang.String" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
female 女性
</body>
</html>
201session内置对象的应用。
(1)设计第1个JSP网页(index1.jsp),页面中为一表单,有2个文本框和一个提交按钮,用于输入姓名、班级,表单提交给第2个网页进行处理。表单中应有相应的标题,如:“请输入学生信息”,文本框前有相应的标签,如:“姓名:”、“班级:”等。 (2)设计第2个JSP网页(readStudent1.jsp),获取第1个网页中用户输入的信息,并将其保存在会话上下文中。网页中添加到第3个JSP网页的链接。 (3)设计第3个JSP网页(showStudent.jsp),获取会话上下文中的2个属性值,并在页面中显示其内容。
index1.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!DOCTYPE html>
<html>
<head>
<meta charset=GB2312>
<title>Insert title here</title>
</head>
<body>
<h1>Please Input Student's Information</h1>
<form method=post action=" readStudent1.jsp">
<table align=" center">
<tr>
<td>name:</td>
<td><input type=text name=name></td>
</tr>
<tr>
<td>class:</td>
<td><input type=text name=class></td>
<tr>
</tr>
<td colspan=2 align= "center">
<input type=submit value="submint">
</td>
</table>
</form>
</body>
</html>
readStudent1.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!DOCTYPE html>
<html>
<head>
<meta charset=GB2312>
<title>readStudent1</title>
</head>
<body>
<%
String name=request.getParameter("name");
String classm=request.getParameter("class");
out.println(name);
session.setAttribute("name", new String(name.getBytes("ISO-8859-1")));
session.setAttribute("class", classm);
//response.sendRedirect("showStudent.jsp");
// String userName = request.getParameter("userName");
// String password = request.getParameter("password");
// out.println("表单输入userName的值:" + new String(userName.getBytes("ISO-8859-1"), "gb2312")+ "<br>");
// out.println("表单输入password的值:" + new String(password.getBytes("ISO-8859-1"), "gb2312")+ "<br>");
%>
<a href="showStudent.jsp">showStudent.jsp</a>
</body>
</html>
showStudent.jsp
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<!DOCTYPE html>
<html>
<head>
<meta charset=GB2312>
<title>Insert title here</title>
</head>
<body>
<h1>Student's Information</h1>
<%
String name = (String)session.getAttribute("name");
String classm = (String)session.getAttribute("class");
%>
<div> <%=name %></div>
<div> <%=classm %></div>
<%session.invalidate(); %>
<br>
方法一:销毁Session对象使用session.invalidate();方法,这样可以销毁当前浏览器的Session对象及其中存放的属性;<br>
方法二:因为判断用户是否登录是将某个属性通过session.setAttribute(fieldName);方法放入session中,然后在其他地方看取出是否是null值。<br>
所以可以使用session.removeAttribute(fieldName);方法清除该属性,从而达到退出的效果,但该方法未真正将Session对象销毁。<br>
关于Session的认识:Session在服务器上创建,且在创建之后会在服务器存在一段时间,当在这段时间内用户无操作的话,则Session会被销毁,<br>
否则Session会重新计算Session在这段时间是否有操作。<br>
当用户登录后且未退出,此时关闭浏览器。当再次打开浏览器,访问之前的页面,发现需要重新登录。<br>
这个现象并不是说关闭浏览器后Session就被销毁了,而是现在的浏览器已经找不到之前的那个Session对象了,<br>
此时需要再次登录,登录后又会创建一个新的Session对象,而之前的Session对象只能等他在生命周期结束后由服务器自动销毁。
</body>
</html>
202Cookie操作
(1)设计第1个JSP网页(cookie.jsp),在其中保存一个Cookie,内容为当前日期时间,至少精确到秒。显示保存Cookie的文本提示信息和到第2个网页(readCookie.jsp)的链接。 (2)设计第2个JSP网页(readCookie.jsp),获取上边第1个网页中保存的Cookie,并在页面中显示其内容。
cookie.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.lang.String" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie</title>
</head>
<body>
<%
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
java.util.Date currentTime = new java.util.Date();//
String str_date1 = formatter.format(currentTime); //••
String str_date2 = currentTime.toString(); //•
Cookie cookie = new Cookie("SESSION_LOGIN_USERNAME",str_date1); //
cookie.setMaxAge(99999999);
response.addCookie(cookie);
cookie.setMaxAge(14*24*60*60);
response.addCookie(cookie);
%>
<div>
<%=str_date1 %>
</div>
<a href = 'readCookie.jsp' >readCookie</a>
</body>
</html>
readCookie.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Read Cookie</title>
</head>
<body>
<h1> Cookie </h1>
<%
String TimeCookie = null;
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if ("SESSION_LOGIN_USERNAME".equals(cookie.getName())) {
TimeCookie = cookie.getValue(); // 徕啰cookie畕畨敷啕
}
}
%>
<div>
</div>
<div><%=TimeCookie %>
</div>
</body>
</html>
203 Servlet设计
(1)设计第1个JSP网页(index2.jsp),页面中为一表单,有2个文本框和一个提交按钮,用于输入姓名、班级,表单提交给下面的Servlet进行处理。表单中应有相应的标题,如:“请输入学生信息”,文本框前有相应的标签,如:“姓名:”、“班级:”等。 (2)设计1个Servlet(命名为StudentServlet.java),获取第1个网页中用户输入的信息,并将其保存在请求上下文中,转到第2个JSP网页readStudent2.jsp。 (3)设计第2个JSP网页(readStudent2.jsp),获取请求上下文中的2个属性值,并在页面中显示其内容。
index2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method=post action="/202/StudentServlet">
<table align="center" cellpadding="4" cellspacing="4">
<tr>
<td colspan="2" align="center"><h3>input</h3></td>
</tr>
<tr>
<td align="right">Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td align="right">Class:</td>
<td><input type="text" name="classm"></td>
</tr>
<tr>
<td colspan=2 align="center">
<input type="submit" value="submit">
</tr>
</table>
</form>
</body>
</html>
readStudent2.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name = (String)session.getAttribute("name");
String classm = (String)session.getAttribute("classm");
%>
<div><%=name %></div>
<div><%=classm %></div>
</body>
</html>
StudentServlet.java
package StudentServlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Servlet implementation class StudentServlet
*/
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
String classm;
String name;
// public void init() throws ServletException {
// //TODO Auto-generated method stub
// classm=getInitParameter("classm");
// name=getInitParameter("name");
// }
/**
* @see HttpServlet#HttpServlet()
*/
public StudentServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// doGet(request, response);
String name = request.getParameter("name");
String classm = request.getParameter("classm");
HttpSession session = request.getSession();
session.setAttribute("name", name);
session.setAttribute("classm", classm);
response.sendRedirect("readStudent2.jsp");
}
}
204 Servlet设计
(1)设计第1个JSP网页,页面中为一表单,有2个文本框和一个提交按钮,用于输入用户名、密码,表单提交给下面的Servlet进行处理。 表单中应有相应的标题,如:“信息录入页面”,文本框前有相应的标签,如:“用户名:”、“密码:”等。 (2)设计1个Servlet,前面的JSP网页提交后必须转向此Servlet;获取第1个网页中用户输入的信息并显示出来。 Servlet中配置2个初始化参数,参数名自定义,参数值分别为你的姓名、学号,在Servlet中获取初始化参数的值并显示出来。
index3.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method=post action="/204/ShowStudent">
<table align="center" cellpadding="4" cellspacing="4">
<tr>
<td colspan="2" align="center"><h3>input</h3></td>
</tr>
<tr>
<td align="right">Name:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td align="right">Class:</td>
<td><input type="text" name="classm"></td>
</tr>
<tr>
<td colspan=2 align="center">
<input type="submit" value="submit">
</tr>
</table>
</form>
</body>
</html>
ShowStudent.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ShowStudent
*/
@WebServlet("/ShowStudent")
public class ShowStudent extends HttpServlet {
private static final long serialVersionUID = 1L;
String classm;
String name;
public void init() throws ServletException {
//TODO Auto-generated method stub
classm=getInitParameter("classm");
name=getInitParameter("name");
}
/**
* @see HttpServlet#HttpServlet()
*/
public ShowStudent() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("name");
String classm=request.getParameter("classm");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response .getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Second Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<div style='width:280px;margin:auto;padding:8px'>");
out.println("<h3>Servlet输出</h3>");
out.println("<hr/>");
out.println("客户端提交的请求参数:<br/>");
out.println("username = " + name + "<br/>");
out.println("password = " + classm + "<br/>");
out.println("<hr/>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
301用户登录功能设计
MySQL数据库服务器为本地localhost;数据库名为:jsptest;其中有一保存用户信息的数据表:jspuser,表结构与保存的用户信息见附图;MySQL用户可使用数据库的管理帐户root,密码为空。要求设计具有简易登录功能的JSP网页,具体内容与要求如下:
- 实体JavaBean,文件名为:JspUser.java;包结构为“yzs(姓名全拼的首字母).test.jsp”;属性与数据表jspuser中的字段一致,用于保存输入的用户信息。
- 连接JavaBean,文件名为:DbConnection.java;包结构同上;实现与数据库连接的静态方法,方法名自定,返回Connection类型的对象。
- 数据访问JavaBean,文件名为:JspUserDao.java;包结构同上;实现登录功能的方法,方法名自定,调用DbConnection.java的静态方法建立与数据库的连接,然后查询数据库检查用户名与密码是否正确,返回boolean类型的值,确定是否登录成功。用户名与密码信息由方法的参数(JspUser.java类型的对象)提供。方法结束前需关闭数据库连接。
- 登录页面,文件名为login.jsp。在页面中设计一表单,有两个文本框和一个提交按钮,用于输入用户名与密码,表单提交给login_act.jsp进行处理。表单下方,根据页面跳转流程中传递的参数,显示相应的提示信息。要求网页中有相应的标题,如:“系统登录”,文本框前有相应的标签,如:“用户名:”、“密码:”等。
- 登录处理页面login_act.jsp,将login.jsp表单中用户输入的信息赋予JspUser Bean对象的相应属性中,判断用户名是否为空,如果为空,转回到login.jsp,在表单下方显示提示信息“Please enter the username!”或者“请输入用户名!”;用户名不为空,以JspUser Bean对象为参数调用JspUserDao对象的方法进行登录,登录不成功,转回到login.jsp,在表单下方显示提示信息“Wrong username or password !”或者“用户名与密码错误!”;登录成功,转到下一个页面main.jsp。转向与参数传递方法自定。
- 目标页面main.jsp,该页面只有登录以后才能显示,在未登录的情况下访问该网页,会转到login.jsp,并在表单下方显示提示信息“Please login!”或者“请登录!”;登录后该页面显示用户名及其它标题信息,如:“登录成功”;在页面适当的位置设计“注销”按钮,点击该按钮,注销用户的登录信息,并转到login.jsp页面。页面访问保护控制代码可写在页面中,也可分离出来写在一个单独的文件中,包含到页面内;
login_act.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="JspUserDao" class="sxj.test.JspUserDao" />
<jps:setProperty name = "JspUserDao" property = "*"/>
<%
String name = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(name);
if (name != null || name != "")
{
boolean user = JspUserDao.login(name,password);
if(user){
//session.setAttribute("loginId", user.getId());
session.setAttribute("loginUser", name);
%>
<jsp:forward page="main.jsp" />
<%
}else{
session.setAttribute("error","1");
%>
<jsp:forward page="login.jsp" />
<%}
}else{
session.setAttribute("error","2");
%>
<jsp:forward page="login.jsp" />
<%}%>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method=post action="login_act.jsp">
<table align="center">
<tr>
<td>username</td>
<td><input type=text name="username"></td>
</tr>
<tr>
<td>password</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan=2 align="center">
<input type=submit value="Login">
<input type=reset value="Cancel">
</td>
</tr>
</table>
<%
String errorcode=(String)session.getAttribute("error");
if("1".equals(errorcode)){
out.println("Wrong username or password");
session.setAttribute("error","0");
}
if("2".equals(errorcode))
{
out.println("Please enter the username");
session.setAttribute("error","0");
}
if("3".equals(errorcode))
{
out.println("Please login!");
session.setAttribute("error","0");
}
%>
</form>
</body>
</html>
main.jsp
<!--
暂不实现
-->
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Login Success</title>
</head>
<body>
<br>
<hr width="380">
<center>
<h1>
<%
String strlogin=(String)session.getAttribute("loginUser");
if(strlogin==null){
session.setAttribute("error","3");
response.sendRedirect("login.jsp");
}
%>
<%out.print(strlogin);%>
Welcome
</h1>
<h2>
Login Success!!!<%= strlogin %>
</h2>
</center>
</body>
</html>
DbConnection.java
package sxj.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConnection {
public static Connection getConnection() {
// TODO Auto-generated method stub
Connection con = null;
String drivername = "com.mysql.jdbc.Driver";
String urlstr = "jdbc:mysql://localhost:3306/jsptest";
String username = "root";
String password = "";
try{
Class.forName(drivername);
} catch (Exception e){
System.out.println("error");
System.out.println(e);
}
try{
con = DriverManager.getConnection(urlstr,username,password);
} catch (SQLException e){
System.out.println("error");
System.out.println(e);
}
return con;
}
}
JspUser.java
package sxj.test;
public class JspUser {
public int id;
public String username;
public String email;
public String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
JspUserDao.java
package sxj.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
//import com.jspdev.experiment.DbConnection;
//import com.jspdev.experiment.UserBean;
import sxj.test.*;
public class JspUserDao {
// 定义私有变量保存连接对象
private Connection con;
// 增加一个用户,传递一个UserBean对象的参数
// 该对象中存储了用户记录的信息
public void addUser(JspUser user) throws Exception {
con = DbConnection.getConnection();
PreparedStatement pstmt = con.prepareStatement("insert into users(name,pwd)values(?,?)");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.executeUpdate();
pstmt.close();
con.close();
}
// 验证登录,传递用户名和密码参数
// 返回保存相应用户信息的UserBean对象,或者null
public boolean login(String Username , String Password) throws Exception {
con = DbConnection.getConnection();
Statement stmt = con.createStatement();
String strSql = "select * from users where username='" +Username+ "' and password='" +Password+ "';";
System.out.println(strSql);
ResultSet rs = stmt.executeQuery(strSql);
JspUser item = null;
while (rs.next()) {
item = new JspUser();
//item.setId(rs.getInt("id"));
item.setUsername(rs.getString("username"));
item.setPassword(rs.getString("password"));
if(Username.equals(rs.getString("username"))&& Password.equals(rs.getString("password"))){
rs.close();
stmt.close();
con.close();
return true;
}
}
return false;
//return item;
}
}
302数据插入操作。
具体内容与要求如下:
- 实体JavaBean,文件名为:JspUser.java;包结构为“yzs(你姓名全拼的首字母).test.jsp”;属性与数据表jspuser中的字段一致,用于保存输入的用户信息。sxj.test.jsp
- 连接JavaBean,文件名为:DbConnection.java;包结构同上;实现与数据库连接的静态方法,方法名自定,返回Connection类型的对象。
- 数据访问JavaBean,文件名为:JspUserDao.java;包结构同上;实现两个方法,第1个方法,方法名自定,参数为JspUser类型的对象。该方法调用DbConnection.java的静态方法建立与数据库的连接,然后查询数据库检查用户名或者电子邮箱是否已存在,返回boolean类型的值。 select * from jspuser where username=? or email=? boolean a=false; if(rs.next()) a=true; return a;
第2个方法,参数同第1个函数,返回整数值,实现注册功能 int a=pt.executeUpate(); return a;
- 注册页面,文件名为register.jsp。在页面中设计一表单,有四个文本框和一个提交按钮,用于输入用户名、电子邮箱、密码与密码确认,表单提交给reg_act.jsp进行处理。表单下方,根据页面运行流程中传递的参数,显示相应的提示信息。要求网页中有相应的标题,如:“用户注册”,文本框前有相应的标签,如:“用户名:”、“电子邮箱:”、“密码:”、“密码确认:”等。
- 注册处理页面reg_act.jsp,将register.jsp表单中用户输入的信息赋予JspUser Bean对象的相应属性中,以JspUser Bean对象为参数调用JspUserDao对象的第1个方法检查用户名或者电子邮箱是否存在,如果存在,转回到register.jsp,在表单下方显示提示信息“The username has existed or email address has already registered!”或者“用户名已存在或者电子邮箱已注册!”;否则,调用JspUserDao对象的第2个方法进行注册,注册成功,转到下一个页面userinfo.jsp;否则,转回到register.jsp,在表单下方显示提示信息“Error occured in database operation! Please tyr again!”或者“数据库操作错误,请重试!”。转向与参数传递方法自定。
(不要求必须实现:在调用JspUserDao的方法之前,最好加上验证,判断用户名、电子邮箱、密码与密码确认是否为空,如果验证未通过,转回到main.jsp,在表单下方显示提示信息“You must input username, email, password and confirm passord!”或者“用户名、电子邮箱、密码和密码确认都必须输入,不能为空!”;否则,继续判断用户名、电子邮箱、密码与密码格式以及长度是否小于数据库中相应字段的长度,如果验证未通过,转回到imain.jsp,在表单下方显示提示信息“You must input username, email, password and confirm passord with english letter, digits or underscore character, and their length must be less 20!”或者“用户名、电子邮箱、密码和密码确认都必须输入字母、数字、下划线,且长度应小于20!”;否则,继续判断电子邮箱格式是否正确(邮箱地址包含@字符),如果不正确,转回到main.jsp,在表单下方显示提示信息“Incorrect email address!”或者“电子邮箱格式不正确!”;否则,继续判断密码与确认密码是否一致,如果不一致,转回到main.jsp,在表单下方显示提示信息“The password and confirm passord are not same!”“两次输入的密码不一致!”。验证部分不作要求,可用客户端JavaScript实现。)
- 注册信息页面userinfo.jsp,在页面显示注册成功提示,并显示用户名、电子邮箱、密码。
DbConnection.java
package sxj.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConnection {
public static Connection getConnection() {
// TODO Auto-generated method stub
Connection con = null;
String drivername = "com.mysql.jdbc.Driver";
String urlstr = "jdbc:mysql://localhost:3306/jsptest";
String username = "root";
String password = "";
try{
Class.forName(drivername);
} catch (Exception e){
System.out.println("error");
System.out.println(e);
}
try{
con = DriverManager.getConnection(urlstr,username,password);
} catch (SQLException e){
System.out.println("error");
System.out.println(e);
}
return con;
}
}
JspUser.java
package sxj.test;
public class JspUser {
public int id;
public String username;
public String email;
public String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
JspUserDao.java
package sxj.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import sxj.test.*;
public class JspUserDao {
private Connection con;
public int add(JspUser user)throws Exception {
//boolean isLogin = false;
con = DbConnection.getConnection();
// String sql = "inset into users(username,email,password) Values(?,?,?)";
PreparedStatement pstmt = con.prepareStatement("insert into users(username,email,password) Values(?,?,?)");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getEmail());
pstmt.setString(3, user.getPassword());
int i = pstmt.executeUpdate();
pstmt.close();
con.close();
//返回更新数据的数量
return i;
}
public boolean login(String Username , String Password) throws Exception {
boolean isLogin = false;
con = DbConnection.getConnection();
Statement stmt = con.createStatement();
String strSql = "select * from users where username='" +Username+ "' and password='" +Password+ "';";
System.out.println(strSql);
ResultSet rs = stmt.executeQuery(strSql);
// JspUser item = null;
// while (rs.next()) {
// item = new JspUser();
// //item.setId(rs.getInt("id"));
// item.setUsername(rs.getString("username"));
// item.setPassword(rs.getString("password"));
// if(Username.equals(rs.getString("username"))&& Password.equals(rs.getString("password"))){
// rs.close();
// stmt.close();
// con.close();
// return true;
// }
// }
// return false;
//session.invalidate();销毁Session
if(rs.next()){
isLogin=true;
}
rs.close();
stmt.close();
con.close();
return isLogin;
//return item;
}
public boolean check(JspUser user)throws Exception{
boolean isLogin = false;
String username= user.getUsername();
String email= user.getEmail();
con = DbConnection.getConnection();
Statement stmt = con.createStatement();
String strSql = "select * from users where username='" +username+ "' or email='" +email+ "';";
System.out.println(strSql);
ResultSet rs = stmt.executeQuery(strSql);
if(rs.next()){
isLogin=true;
}
rs.close();
stmt.close();
con.close();
return isLogin;
}
}
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String strlogin=(String)session.getAttribute("loginUser");
if(strlogin==null){
session.setAttribute("error","3");
response.sendRedirect("login.jsp");
}
%>
<%out.print(strlogin);%>
Welcome••
</h1>
<h2>Login Success!!!<%= strlogin %></h2>
</center>
</body>
</html>
reg_act.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="sxj.test.JspUserDao"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="JspUserDao" class="sxj.test.JspUserDao" />
<jsp:useBean id="JspUser" scope="page" class="sxj.test.JspUser" />
<%String name = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");%>
<jsp:setProperty name="JspUser" property="*" />
<%
// javax.servlet.ServletException: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
//You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near
//'inset into users(username,email,password) Values('test3','test3@123.com','123')' at line 1
//if(name == null || name == ""|| password == null || password == ""||email == null || email == ""){ }
String info = "";
if(JspUserDao.check(JspUser)){
info = "用户名已存在或者电子邮箱已注册!";
request.setAttribute("info",info);%>
<jsp:forward page="register.jsp" />
<%}else{
if(JspUserDao.add(JspUser)>0){
session.setAttribute("name", name);
session.setAttribute("password", password);
session.setAttribute("email", email);
%><jsp:forward page="userinfo.jsp" /><%
}else{
info = "数据库操作错误,请重试!";
request.setAttribute("info",info);
%>
<jsp:forward page="register.jsp" />
<%
}
}%>
</body>
</html>
register.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form method=post action="reg_act.jsp">
<table align="center">
<tr>
<td colspan=2 align="center">
<h1>register</h1>
</td>
</tr>
<tr>
<td>username</td>
<td><input type=text name="username"></td>
</tr>
<tr>
<td>email</td>
<td><input type=text name="email"></td>
</tr>
<tr>
<td>password</td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td>Password Confirm:</td>
<td>
<input type="password" name="password1" id="password1" onkeyup="validate()" >
<span id="tishi"></span>
</td>
</tr>
<tr>
<td colspan=2 align="center">
<input type=submit value="Login" id="button" style="width: 108px">
<!-- <input type=reset value="Cancel"> -->
</td>
</tr>
<tr>
<td colspan=2 align="center">
<%=request.getAttribute("info")==null?"":request.getAttribute("info")%>
</td>
</tr>
</table>
</form>
<script>
function validate() {
var pwd1 = document.getElementById("password").value;
var pwd2 = document.getElementById("password1").value;
<!-- 对比两次输入的密码 -->
if(pwd1 == pwd2)
{
document.getElementById("tishi").innerHTML="<font color='green'>两次密码相同</font>";
document.getElementById("button").disabled = false;
}
else {
document.getElementById("tishi").innerHTML="<font color='red'>两次密码不相同</font>";
document.getElementById("button").disabled = true;
}
}
</script>
</body>
</html>
userinfo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<style type="text/css">
ul {
list-style: none; /*设置不显示项目符号*/
margin: 0px; /*设置外边距*/
padding: 5px; /*设置内边距*/
}
li {
padding: 5px; /*设置内边距*/
}
</style>
<jsp:useBean id="JspUser" scope="page" class="sxj.test.JspUser" />
<table align="center" style="width: 320px; ">
<tr>
<td colspan=2 align="center">
<h1>UserInfo</h1>
</td>
</tr>
<tr>
<td>username</td>
<td><%=session.getAttribute("name")%></td>
</tr>
<tr>
<td>email</td>
<td><%=session.getAttribute("email")%></td>
</tr>
<tr>
<td>password</td>
<td><%=session.getAttribute("password")%></td>
</tr>
</table>
</body>
</html>
303数据库查询。
要求设计查询数据库,浏览用户信息的JSP网页,具体内容与要求如下:
- 实体JavaBean,文件名为:JspUser.java;包结构为“yzs(你姓名全拼的首字母).test.jsp”;属性与数据表jspuser中的字段一致,用于保存输入的用户信息。
- 连接JavaBean,文件名为:DbConnection.java;包结构同上;实现与数据库连接的静态方法,方法名自定,返回Connection类型的对象。
- 数据访问JavaBean,文件名为:JspUserDao.java;包结构同上;实现三个方法,第1个方法,方法名自定,无参数。该方法调用DbConnection.java的静态方法建立与数据库的连接,然后查询数据库检索jspuser表中的所有记录,每条记录的数据封装在一个JspUser Bean中,所有的JspUser Bean对象组成一个List对象,返回List对象。第2个方法,参数为用户ID,整数类型,从jspuser表中查询指定ID的用户记录,将记录中的用户信息封装到JspUser Bean中,返回该JspUser Bean对象。第3个方法,参数为字符串,查询jspuser数据表中用户名包含字符串参数的所有记录,每条记录的数据封装在一个JspUser Bean中,所有的JspUser Bean对象组成一个List对象,返回List对象。方法结束前需关闭数据库连接。import java.util.*;
public ArrayList<JspUser> getAllUser() throws Exception {Statement stmt = con.createStatement();
ArrayList<JspUser> data = new ArrayList<JspUser>();
String strSql = "select * from jspuser";
ResultSet rs = stmt.executeQuery(strSql);
while (rs.next()) {
JspUser item = new JspUser();
item.setId(rs.getInt("id"));
item.setUsername(rs.getString("username"));
item.setPassword(rs.getString("password"));
item.setEmail(rs.getString("email"));
data.add(item);
}
rs.close();
stmt.close();
con.close();
return data;
}
PreparedStatement ptmt = con.prepareStatement("select * from jspuser where username like ?");
ptmt.setString(1, "%" + name + "%"); - 用户信息浏览页面,文件名为main.jsp。在页面中引入JspUserDao Bean,调用其第1个方法,获取保存所有用户信息的List对象,循环遍历List对象中所有的元素和每个元素(JspUser 对象)中的所有属性,用表格显示出来,每条记录(JspUser 中保存的用户信息)为一行,每个字段(JspUser Bean中的每个属性)为一列,每行的最后一列为“详细”链接,链接到detail.jsp页面,传递用户ID参数。用户信息列表下面为一表单,有一文本框和提交按钮,表单提交给search.jsp,模糊查询用户名包含文本框中用户输入内容的所有用户。
ArrayList<JspUser> data = jspexdb.getAllUser();
Iterator<JspUser> it = data.iterator();
while (it.hasNext()) {
JspUser temp = it.next();
int id=temp.getId();
out.println("<td>"+"<a href=detail.jsp?id="+id+">详细</a>"+"</td>");}
或者for(int i=0; i<data.size(); i++) {
JspUser item =data.get(i);
- 用户详细信息页面detail.jsp,获取main.jsp传递的用户ID参数,引入JspUserDao Bean,调用其第2个方法,获取id为参数值的记录,返回保存该用户信息的JspUser对象,用表格显示用户信息,每个字段为一行。
- 用户模糊查询页面search.jsp,获取main.jsp提交的用户在文本框中输入的参数,引入JspUserDao Bean,调用其第3个方法,查询用户名包含main.jsp提交的参数的所有用户,返回封装用户数据的List对象,循环遍历List对象中所有的元素和每个元素(JspUser对象)中的所有属性,用表格显示出来,每条记录(JspUser中保存的用户信息)为一行,每个字段(JspUser Bean中的每个属性)为一列。
DbConnection.java
package sxj.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConnection {
public static Connection getConnection() {
// TODO Auto-generated method stub
Connection con = null;
String drivername = "com.mysql.jdbc.Driver";
String urlstr = "jdbc:mysql://localhost:3306/jsptest";
String username = "root";
String password = "";
try{
Class.forName(drivername);
} catch (Exception e){
System.out.println("error");
System.out.println(e);
}
try{
con = DriverManager.getConnection(urlstr,username,password);
} catch (SQLException e){
System.out.println("error");
System.out.println(e);
}
return con;
}
}
JspUser.java
package sxj.test;
public class JspUser {
public int id;
public String username;
public String email;
public String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
JspUserDao.java
package sxj.test.jsp;
import java.sql.Connection;
import java.util.*;
import sxj.test.jsp.DbConnection;
import sxj.test.jsp.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
public class JspUserDao {
private Connection con;
public ArrayList<JspUser> getAllUser() throws Exception {
con = DbConnection.getConnection();
Statement stmt = con.createStatement();
//con = DbConnection.getConnection();
//Statement stmt = con.createStatement();
ArrayList<JspUser> data = new ArrayList<JspUser>();
String strSql = "select * from users";
ResultSet rs = stmt.executeQuery(strSql);
while (rs.next()) {
JspUser item = new JspUser();
item.setId(rs.getInt("id"));
item.setUsername(rs.getString("username"));
item.setPassword(rs.getString("password"));
item.setEmail(rs.getString("email"));
data.add(item);
}
rs.close();
stmt.close();
con.close();
return data;
}
public JspUser getuser(int id)throws Exception{
JspUser item = new JspUser();
// ArrayList<JspUser> data = new ArrayList<JspUser>();
con = DbConnection.getConnection();
PreparedStatement ptmt = con.prepareStatement("select * from users where id=?");
ptmt.setString(1,Integer.toString(id));
ResultSet rs=ptmt.executeQuery();
while (rs.next()) {
item.setId(rs.getInt("id"));
item.setUsername(rs.getString("username"));
item.setPassword(rs.getString("password"));
item.setEmail(rs.getString("email"));
}
rs.close();
ptmt.close();
con.close();
return item;
}
public ArrayList<JspUser> userAll(String name)throws Exception{
ArrayList<JspUser> data = new ArrayList<JspUser>();
con = DbConnection.getConnection();
PreparedStatement ptmt = con.prepareStatement("select * from users where username like ?");
ptmt.setString(1, "%" + name + "%");
ResultSet rs=ptmt.executeQuery();
while (rs.next()) {
JspUser item = new JspUser();
item.setId(rs.getInt("id"));
item.setUsername(rs.getString("username"));
item.setPassword(rs.getString("password"));
item.setEmail(rs.getString("email"));
data.add(item);
}
return data;
}
}
detail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="sxj.test.jsp.JspUserDao"%>
<%@ page import="sxj.test.jsp.JspUser"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="JspUser" scope="page" class="sxj.test.jsp.JspUser" />
<jsp:useBean id="JspUserDao" class="sxj.test.jsp.JspUserDao" />
<%
String id = "1";
id = request.getParameter("id");
JspUser data = JspUserDao.getuser(Integer.parseInt(id));
int idu =data.getId();
String username = data.getUsername();
String email = data.getEmail();
String password = data.getPassword();
out.println("<table align='center' border='6'>");
out.println("<caption><h1>UserInfo</h1></caption>");
out.println("<tr><td>id</td><td>"+id+"</td></tr>");
out.println("<tr><td>username</td><td>"+username+"</td></tr>");
out.println("<tr><td>email</td><td>"+email+"</td></tr>");
out.println("<tr><td>password</td><td>"+password+"</td></tr>");
out.println("</table>");
%>
</body>
</html>
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="sxj.test.jsp.JspUserDao"%>
<%@ page import="sxj.test.jsp.JspUser"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="JspUser" scope="page" class="sxj.test.jsp.JspUser" />
<jsp:useBean id="JspUserDao" class="sxj.test.jsp.JspUserDao" />
<%
List<JspUser> data = JspUserDao.getAllUser();
Iterator<JspUser> it = data.iterator();
out.println("<table align='center' border='6'>");
out.println("<caption><h1>UserList</h1></caption>");
out.println("<tr><th>id</th><th>username</th><th>email</th><th>password</th><th>详细</th></tr>");
while (it.hasNext()) {
JspUser temp = it.next();
int id=temp.getId();
String username = temp.getUsername();
String email = temp.getEmail();
String password = temp.getPassword();
// out.println(id+" | ");
// out.println(username+" | ");
// out.println(email+" | ");
// out.println(password+" | ");
// out.println("<tr><td>id</td><td>"+id+"</td></tr>");
out.println("<tr><td>"+id+"</td><td>"+username+"</td><td>"+email+"</td><td>"+password+"</td><td>"+"<a href=detail.jsp?id="+id+">详细</a>"+"</td></tr>");
// out.println("<tr><td>email</td><td>"+email+"</td></tr>");
// out.println("<tr><td>password</td><td>"+password+"</td></tr>");
// out.println("<td>"+"<a href=detail.jsp?id="+id+">详细</a>"+"</td>");
//out.println("<br>");
}
out.println("</table>");
%>
<form method=post action="search.jsp" >
<br>
<div align='center'>
用户名搜索:
<input type=text name="username">
<input type=submit value="Search" id="button" style="width: 108px; ">
</div>
</form>
</body>
</html>
search.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="sxj.test.jsp.JspUserDao"%>
<%@ page import="sxj.test.jsp.JspUser"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="JspUser" scope="page" class="sxj.test.jsp.JspUser" />
<jsp:useBean id="JspUserDao" class="sxj.test.jsp.JspUserDao" />
<%
String username = "test";
username = request.getParameter("username");
//JspUser data = JspUserDao.getuser(Integer.parseInt(id));
List<JspUser> data = JspUserDao.userAll(username);
Iterator<JspUser> it = data.iterator();
out.println("<table align='center' border='6'>");
out.println("<caption><h1>Search Result</h1></caption>");
out.println("<tr><th>id</th><th>username</th><th>email</th><th>password</th><th>详细</th></tr>");
while (it.hasNext()) {
JspUser temp = it.next();
int id=temp.getId();
String usernamef = temp.getUsername();
String email = temp.getEmail();
String password = temp.getPassword();
// out.println(id+" | ");
// out.println(username+" | ");
// out.println(email+" | ");
// out.println(password+" | ");
// out.println("<tr><td>id</td><td>"+id+"</td></tr>");
out.println("<tr><td>"+id+"</td><td>"+usernamef+"</td><td>"+email+"</td><td>"+password+"</td><td>"+"<a href=detail.jsp?id="+id+">详细</a>"+"</td></tr>");
// out.println("<tr><td>email</td><td>"+email+"</td></tr>");
// out.println("<tr><td>password</td><td>"+password+"</td></tr>");
// out.println("<td>"+"<a href=detail.jsp?id="+id+">详细</a>"+"</td>");
//out.println("<br>");
}
out.println("</table>");
%>
</body>
</html>
304数据更新操作。
具体内容与要求如下:
- 实体JavaBean,文件名为:JspUser.java;包结构为“yzs(你姓名全拼的首字母).test.jsp”;属性与数据表jspuser中的字段一致,用于保存输入的用户信息。
- 连接JavaBean,文件名为:DbConnection.java;包结构同上;实现与数据库连接的静态方法,方法名自定,返回Connection类型的对象。
- 数据访问JavaBean,文件名为:JspUserDao.java;包结构同上;实现三个方法,第1个方法,方法名自定,无参数。该方法调用DbConnection.java的静态方法建立与数据库的连接,然后查询数据库检索jspuser表中的所有记录,每条记录的数据封装在一个JspUser Bean中,所有的JspUser Bean对象组成一个List对象,返回List对象。第2个方法,参数为用户ID,整数类型,从jspuser表中查询指定ID的用户记录,将记录中的用户信息封装到JspUser Bean中,返回该JspUser Bean对象。第3个方法,参数为JspUser Bean对象,返回整数值,实现该用户信息的更新功能。
- 用户信息浏览页面,文件名为main.jsp。在页面中引入JspUserDao Bean,调用其第1个方法,获取保存所有用户信息的List对象,循环遍历List对象中所有的元素和每个元素(JspUser 对象)中的所有属性,用表格显示出来,每条记录(JspUser 中保存的用户信息)为一行,每个字段(JspUser Bean中的每个属性)为一列,每行的最后一列为修改链接,链接到userupdate.jsp页面,传递用户ID参数。
- 更新页面userupdate.jsp,获取main.jsp传递的用户ID参数,引入JspUserDao Bean,调用其第2个方法,获取保存要更新的用户信息的User对象,在页面中设计一表单,有四个文本框和一个提交按钮,用于输入用户名、电子邮箱、密码与密码确认,用JspUser对象的属性值填充相应的文本框。表单提交给update_act.jsp进行处理。要求网页中有相应的标题,如:“用户信息更新”,文本框前有相应的标签,如:“用户名:”、“电子邮箱:”、“密码:”、“密码确认:”等<%
String id=request.getParameter("id");
JspUser udata=dbop.getUserById(id);
%>
<input type="hidden" name="id" value="<%=udata.getId() %>">
最好加上验证,判断用户名、电子邮箱、密码与密码确认是否为空,如果验证未通过,转回到userupdate.jsp,在表单下方显示提示信息“You must input username, email, password and confirm passord!”或者“用户名、电子邮箱、密码和密码确认都必须输入,不能为空!”;否则,继续判断用户名、电子邮箱、密码与密码格式以及长度是否小于数据库中相应字段的长度,如果验证未通过,转回到userupdate.jsp,在表单下方显示提示信息“You must input username, email, password and confirm passord with english letter, digits or underscore character, and their length must be less 20!”或者“用户名、电子邮箱、密码和密码确认都必须输入字母、数字、下划线,且长度应小于20!”;否则,继续判断电子邮箱格式是否正确(邮箱地址包含@字符),如果不正确,转回到userupdate.jsp,在表单下方显示提示信息“Incorrect email address!”或者“电子邮箱格式不正确!”;否则,继续判断密码与确认密码是否一致,如果不一致,转回到userupdate.jsp,在表单下方显示提示信息“The password and confirm passord are not same!”“两次输入的密码不一致!”。验证部分不作要求,也可用客户端JavaScript实现。
- 更新处理页面update_act.jsp,将userupdate.jsp表单中用户输入的信息赋予JspUser Bean对象中,以JspUser Bean对象为参数调用JspUserDao对象的第3个方法更新用户信息,更新成功,转到main.jsp,否则,转回到userupdate.jsp,在表单下方显示提示信息“Error occured in database operation! Please tyr again!”或者“数据库操作错误,请重试!” <jsp:useBean id="user" class="yzs.test.jsp.JspUser" scope="request" /> <jsp:setProperty name="user" property="*" /> <jsp:useBean id="dbop" class="yzs.test.jsp.JspUserDao" /> if (dbop.modifyUser(user) > 0) {}
DbConnection.java
package sxj.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConnection {
public static Connection getConnection() {
// TODO Auto-generated method stub
Connection con = null;
String drivername = "com.mysql.jdbc.Driver";
String urlstr = "jdbc:mysql://localhost:3306/jsptest";
String username = "root";
String password = "";
try{
Class.forName(drivername);
} catch (Exception e){
System.out.println("error");
System.out.println(e);
}
try{
con = DriverManager.getConnection(urlstr,username,password);
} catch (SQLException e){
System.out.println("error");
System.out.println(e);
}
return con;
}
}
JspUser.java
package sxj.test;
public class JspUser {
public int id;
public String username;
public String email;
public String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
JspUserDao.java
package sxj.test.jsp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import sxj.test.jsp.DbConnection;
import sxj.test.jsp.JspUser;
public class JspUserDao {
private Connection con;
public ArrayList<JspUser> getAllUser() throws Exception {
con = DbConnection.getConnection();
Statement stmt = con.createStatement();
//con = DbConnection.getConnection();
//Statement stmt = con.createStatement();
ArrayList<JspUser> data = new ArrayList<JspUser>();
String strSql = "select * from users";
ResultSet rs = stmt.executeQuery(strSql);
while (rs.next()) {
JspUser item = new JspUser();
item.setId(rs.getInt("id"));
item.setUsername(rs.getString("username"));
item.setPassword(rs.getString("password"));
item.setEmail(rs.getString("email"));
data.add(item);
}
rs.close();
stmt.close();
con.close();
return data;
}
public JspUser getuser(int id)throws Exception{
JspUser item = new JspUser();
// ArrayList<JspUser> data = new ArrayList<JspUser>();
con = DbConnection.getConnection();
PreparedStatement ptmt = con.prepareStatement("select * from users where id=?");
ptmt.setString(1,Integer.toString(id));
ResultSet rs=ptmt.executeQuery();
while (rs.next()) {
item.setId(rs.getInt("id"));
item.setUsername(rs.getString("username"));
item.setPassword(rs.getString("password"));
item.setEmail(rs.getString("email"));
}
rs.close();
ptmt.close();
con.close();
return item;
}
public ArrayList<JspUser> userAll(String name)throws Exception{
ArrayList<JspUser> data = new ArrayList<JspUser>();
con = DbConnection.getConnection();
PreparedStatement ptmt = con.prepareStatement("select * from users where username like ?");
ptmt.setString(1, "%" + name + "%");
ResultSet rs=ptmt.executeQuery();
while (rs.next()) {
JspUser item = new JspUser();
item.setId(rs.getInt("id"));
item.setUsername(rs.getString("username"));
item.setPassword(rs.getString("password"));
item.setEmail(rs.getString("email"));
data.add(item);
}
return data;
}
public int upDate(JspUser user)throws Exception {
//boolean isLogin = false;
con = DbConnection.getConnection();
// String sql = "inset into users(username,email,password) Values(?,?,?)"; UPDATE Person SET FirstName = 'Fred' WHERE LastName = 'Wilson'
PreparedStatement pstmt = con.prepareStatement("UPDATE users SET username=?,email=?,password=? WHERE id = ?");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getEmail());
pstmt.setString(3, user.getPassword());
pstmt.setString(4, Integer.toString(user.getId()));
int i = pstmt.executeUpdate();
pstmt.close();
con.close();
//返回更新数据的数量
return i;
}
public boolean FormatEmail(String str)throws Exception{
//String str = "";
String pattern = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
if(m.matches()) {
return true;
}else {
return false;
}
}
// public int getcolength(String columns)throws Exception{
// ///select length('fdasfsafasf');
// int length = 0;
// con = DbConnection.getConnection();
// PreparedStatement ptmt = con.prepareStatement("select length(?) from users");
// ptmt.setString(1,columns);
// ResultSet rs=ptmt.executeQuery();
//
//
//
// return length;
// }
}
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="sxj.test.jsp.JspUserDao"%>
<%@ page import="sxj.test.jsp.JspUser"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="JspUser" scope="page" class="sxj.test.jsp.JspUser" />
<jsp:useBean id="JspUserDao" class="sxj.test.jsp.JspUserDao" />
<%
List<JspUser> data = JspUserDao.getAllUser();
Iterator<JspUser> it = data.iterator();
out.println("<table align='center' border='6'>");
out.println("<caption><h1>UserList</h1></caption>");
out.println("<tr><th>id</th><th>username</th><th>email</th><th>password</th><th>详细</th></tr>");
while (it.hasNext()) {
JspUser temp = it.next();
int id=temp.getId();
String username = temp.getUsername();
String email = temp.getEmail();
String password = temp.getPassword();
// out.println(id+" | ");
// out.println(username+" | ");
// out.println(email+" | ");
// out.println(password+" | ");
// out.println("<tr><td>id</td><td>"+id+"</td></tr>");
out.println("<tr><td>"+id+"</td><td>"+username+"</td><td>"+email+"</td><td>"+password+"</td><td>"+"<a href=userupdate.jsp?id="+id+">修改</a>"+"</td></tr>");
// out.println("<tr><td>email</td><td>"+email+"</td></tr>");
// out.println("<tr><td>password</td><td>"+password+"</td></tr>");
// out.println("<td>"+"<a href=detail.jsp?id="+id+">详细</a>"+"</td>");
//out.println("<br>");
}
out.println("</table>");
%>
</body>
</html>
update_act.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="sxj.test.jsp.JspUserDao"%>
<%@ page import="sxj.test.jsp.JspUser"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="JspUser" scope="page" class="sxj.test.jsp.JspUser" />
<jsp:useBean id="JspUserDao" class="sxj.test.jsp.JspUserDao" />
<jsp:setProperty name="JspUser" property="*" />
<%
String id = request.getParameter("id");
String name = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email");
String info = "";%>
<%
//select length('fdasfsafasf');
// String format = "\\p{Alpha}\\w{2,15}[@][a-z0-9]{3,}[.]\\p{Lower}{2,}";
//email.matches(format)
String format = "\\p{Alpha}\\w{2,15}[@][a-z0-9]{3,}[.]\\p{Lower}{2,}";
if(name == null || name == ""|| password == null || password == ""||email == null || email == ""){
info = "用户名、电子邮箱、密码和密码确认都必须输入,不能为空!";
request.setAttribute("info",info);%>
<jsp:forward page="userupdate.jsp" /><%
}else if(name.length()>20||password.length()>20||email.length()>20){
info = "用户名、电子邮箱、密码和密码确认都必须输入字母、数字、下划线,且长度应小于20!";
request.setAttribute("info",info);
%><jsp:forward page="userupdate.jsp" />
<%
}else if(JspUserDao.FormatEmail(email)){
//id = request.getParameter("id");
int a = JspUserDao.upDate(JspUser);
out.println(name);
out.println(a);
%><jsp:forward page="main.jsp" />
<%
}else{
info = "电子邮箱格式不正确!";
request.setAttribute("info",info);
%><jsp:forward page="userupdate.jsp" />
<%
}
%>
</body>
</html>
userupdate.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="sxj.test.jsp.JspUserDao"%>
<%@ page import="sxj.test.jsp.JspUser"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="JspUser" scope="page" class="sxj.test.jsp.JspUser" />
<jsp:useBean id="JspUserDao" class="sxj.test.jsp.JspUserDao" />
<%
String id = "1";
id = request.getParameter("id");
JspUser data = JspUserDao.getuser(Integer.parseInt(id));
int idu =data.getId();
String username = data.getUsername();
String email = data.getEmail();
String password = data.getPassword();
/*
out.println("<table align='center' border='6'>");
out.println("<caption><h1>UserInfo</h1></caption>");
out.println("<tr><td>id</td><td>"+id+"</td></tr>");
out.println("<tr><td>username</td><td>"+username+"</td></tr>");
out.println("<tr><td>email</td><td>"+email+"</td></tr>");
out.println("<tr><td>password</td><td>"+password+"</td></tr>");
out.println("</table>");*/
%>
<form method=post action="update_act.jsp">
<table align="center">
<tr>
<td colspan=2 align="center">
<h1>Updata</h1>
</td>
</tr>
<tr>
<td>id</td>
<td><input type=text name="id" value="<%=id %>" readonly="readonly" ></td>
</tr>
<tr>
<td>username</td>
<td><input type=text name="username" value="<%=username %>"></td>
</tr>
<tr>
<td>email</td>
<td><input type=text name="email" value="<%=email %>"></td>
</tr>
<tr>
<td>password</td>
<td><input type="password" name="password" id="password" value="<%=password %>"></td>
</tr>
<tr>
<td>Password Confirm:</td>
<td>
<input type="password" name="password1" id="password1" value="<%=password %>" onkeyup="validate()">
<span id="tishi"></span>
</td>
</tr>
<tr>
<td colspan=2 align="center">
<input type=submit value="UpDate" id="button" style="width: 108px">
<!-- <input type=reset value="Cancel"> -->
</td>
<tr>
<td colspan=2 align="center">
<%=request.getAttribute("info")==null?"":request.getAttribute("info")%>
</td>
</tr>
</table>
</form>
<script>
function validate() {
var pwd1 = document.getElementById("password").value;
var pwd2 = document.getElementById("password1").value;
<!-- 对比两次输入的密码 -->
if(pwd1 == pwd2)
{
document.getElementById("tishi").innerHTML="<font color='green'>两次密码相同</font>";
document.getElementById("button").disabled = false;
}
else {
document.getElementById("tishi").innerHTML="<font color='red'>两次密码不相同</font>";
document.getElementById("button").disabled = true;
}
}
</script>
</body>
</html>
305数据删除操作
具体内容与要求如下:
- 实体JavaBean,文件名为:JspUser.java;包结构为“yzs(你姓名全拼的首字母).test.jsp”;属性与数据表jspuser中的字段一致,用于保存输入的用户信息。
- 连接JavaBean,文件名为:DbConnection.java;包结构同上;实现与数据库连接的静态方法,方法名自定,返回Connection类型的对象。
- 数据访问JavaBean,文件名为:JspUserDao.java;包结构同上;实现两个方法,第1个方法,方法名自定,无参数。该方法调用DbConnection.java的静态方法建立与数据库的连接,然后查询数据库检索jspuser表中的所有记录,每条记录的数据封装在一个JspUser Bean中,所有的JspUser Bean对象组成一个List对象,返回List对象。第2个方法,参数为用户ID,整数类型,从jspuser表中删除指定ID的用户记录,返回整数类型。
- 用户信息浏览页面,文件名为main.jsp。在页面中引入JspUserDao Bean,调用其第1个方法,获取保存所有用户信息的List对象,循环遍历List对象中所有的元素和每个元素(JspUser对象)中的所有属性,用表格显示出来,每条记录(JspUser中保存的用户信息)为一行,每个字段(JspUser Bean中的每个属性)为一列,每行的最后一列为删除链接,链接到userdelete.jsp页面,传递用户ID参数。
- 链接的click事件弹出确认对话框,只有点击确定按钮才进行转向,执行删除操作,点击取消按钮则取消转向操作,并不执行删除操作。<%
ArrayList<JspUser> udata= dbop.getAllUser();
for(int i=0; i<udata.size(); i++) {
JspUser item =udata.get(i);
%>
<tr align="center">
<td><%=item.getId() %></td>
<td><%=item.getUsername()%></td>
<td><%=item.getEmail() %></td>
<td><%=item.getPassword()%></td>
<td><a href="userdelete.jsp?id=<%=item.getId()%>" onclick="return confirm('确定要删除该用户吗?')">删除</a></td>
</tr>
<% } %> - 删除页面userdelete.jsp,获取main.jsp传递的用户ID参数,引JspUserDao Bean,调用其第2个方法,删除指定ID的用户信息,转回到main.jsp。
DbConnection.java
package sxj.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbConnection {
public static Connection getConnection() {
// TODO Auto-generated method stub
Connection con = null;
String drivername = "com.mysql.jdbc.Driver";
String urlstr = "jdbc:mysql://localhost:3306/jsptest";
String username = "root";
String password = "";
try{
Class.forName(drivername);
} catch (Exception e){
System.out.println("error");
System.out.println(e);
}
try{
con = DriverManager.getConnection(urlstr,username,password);
} catch (SQLException e){
System.out.println("error");
System.out.println(e);
}
return con;
}
}
JspUser.java
package sxj.test;
public class JspUser {
public int id;
public String username;
public String email;
public String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
JspUserDao.java
package sxj.test.jsp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JspUserDao {
private Connection con;
public ArrayList<JspUser> getAllUser() throws Exception {
con = DbConnection.getConnection();
Statement stmt = con.createStatement();
//con = DbConnection.getConnection();
//Statement stmt = con.createStatement();
ArrayList<JspUser> data = new ArrayList<JspUser>();
String strSql = "select * from users";
ResultSet rs = stmt.executeQuery(strSql);
while (rs.next()) {
JspUser item = new JspUser();
item.setId(rs.getInt("id"));
item.setUsername(rs.getString("username"));
item.setPassword(rs.getString("password"));
item.setEmail(rs.getString("email"));
data.add(item);
}
rs.close();
stmt.close();
con.close();
return data;
}
public JspUser getuser(int id)throws Exception{
JspUser item = new JspUser();
// ArrayList<JspUser> data = new ArrayList<JspUser>();
con = DbConnection.getConnection();
PreparedStatement ptmt = con.prepareStatement("select * from users where id=?");
ptmt.setString(1,Integer.toString(id));
ResultSet rs=ptmt.executeQuery();
while (rs.next()) {
item.setId(rs.getInt("id"));
item.setUsername(rs.getString("username"));
item.setPassword(rs.getString("password"));
item.setEmail(rs.getString("email"));
}
rs.close();
ptmt.close();
con.close();
return item;
}
public ArrayList<JspUser> userAll(String name)throws Exception{
ArrayList<JspUser> data = new ArrayList<JspUser>();
con = DbConnection.getConnection();
PreparedStatement ptmt = con.prepareStatement("select * from users where username like ?");
ptmt.setString(1, "%" + name + "%");
ResultSet rs=ptmt.executeQuery();
while (rs.next()) {
JspUser item = new JspUser();
item.setId(rs.getInt("id"));
item.setUsername(rs.getString("username"));
item.setPassword(rs.getString("password"));
item.setEmail(rs.getString("email"));
data.add(item);
}
return data;
}
public int upDate(JspUser user)throws Exception {
//boolean isLogin = false;
con = DbConnection.getConnection();
// String sql = "inset into users(username,email,password) Values(?,?,?)"; UPDATE Person SET FirstName = 'Fred' WHERE LastName = 'Wilson'
PreparedStatement pstmt = con.prepareStatement("UPDATE users SET username=?,email=?,password=? WHERE id = ?");
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getEmail());
pstmt.setString(3, user.getPassword());
pstmt.setString(4, Integer.toString(user.getId()));
int i = pstmt.executeUpdate();
pstmt.close();
con.close();
//返回更新数据的数量
return i;
}
public boolean FormatEmail(String str)throws Exception{
//String str = "";
String pattern = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(str);
if(m.matches()) {
return true;
}else {
return false;
}
}
public int DeleteUser(int id)throws Exception{
//boolean isLogin = false;
//DELETE FROM w3cschool_tbl WHERE w3cschool_id=3;
con = DbConnection.getConnection();
// String sql = "inset into users(username,email,password) Values(?,?,?)"; UPDATE Person SET FirstName = 'Fred' WHERE LastName = 'Wilson'
PreparedStatement pstmt = con.prepareStatement("DELETE FROM users WHERE id=?");
// pstmt.setString(4, Integer.toString(user.getId()));
pstmt.setString(1, Integer.toString(id));
int i = pstmt.executeUpdate();
pstmt.close();
con.close();
//返回更新数据的数量
return i;
}
}
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="sxj.test.jsp.JspUserDao"%>
<%@ page import="sxj.test.jsp.JspUser"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="JspUser" scope="page" class="sxj.test.jsp.JspUser" />
<jsp:useBean id="JspUserDao" class="sxj.test.jsp.JspUserDao" />
<%
List<JspUser> data = JspUserDao.getAllUser();
Iterator<JspUser> it = data.iterator();
out.println("<table align='center' border='6'>");
out.println("<caption><h1>UserList</h1></caption>");
out.println("<tr><th>id</th><th>username</th><th>email</th><th>password</th><th>详细</th></tr>");
while (it.hasNext()) {
JspUser temp = it.next();
int id=temp.getId();
String username = temp.getUsername();
String email = temp.getEmail();
String password = temp.getPassword();
// out.println(id+" | ");
// out.println(username+" | ");
// out.println(email+" | ");
// out.println(password+" | ");
// out.println("<tr><td>id</td><td>"+id+"</td></tr>");
out.println("<tr><td>"+id+"</td><td>"+username+"</td><td>"+email+"</td><td>"+password+"</td><td>"+"<a href=userdelete.jsp?id="+id+" onclick=\"return confirm('确定要删除该用户吗?')\">删除</a>"+"</td></tr>");
// out.println("<tr><td>email</td><td>"+email+"</td></tr>");
// out.println("<tr><td>password</td><td>"+password+"</td></tr>");
// out.println("<td>"+"<a href=detail.jsp?id="+id+">详细</a>"+"</td>");
//out.println("<br>");
}
out.println("</table>");
%>
<div align = "center"><%=request.getAttribute("info")==null?"":request.getAttribute("info")%></div>
</body>
</html>
userdelete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="sxj.test.jsp.JspUserDao"%>
<%@ page import="sxj.test.jsp.JspUser"%>
<%@ page import="java.util.*"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:useBean id="JspUser" scope="page" class="sxj.test.jsp.JspUser" />
<jsp:useBean id="JspUserDao" class="sxj.test.jsp.JspUserDao" />
<%
String id = "1";
String info = "";
id = request.getParameter("id");
int result = JspUserDao.DeleteUser(Integer.parseInt(id));
if(result>0){
info = "删除成功!";
request.setAttribute("info",info);
%><jsp:forward page="main.jsp" />
<%
}else{
info = "删除失败!";
request.setAttribute("info",info);
%><jsp:forward page="main.jsp" />
<%
}
/*
out.println("<table align='center' border='6'>");
out.println("<caption><h1>UserInfo</h1></caption>");
out.println("<tr><td>id</td><td>"+id+"</td></tr>");
out.println("<tr><td>username</td><td>"+username+"</td></tr>");
out.println("<tr><td>email</td><td>"+email+"</td></tr>");
out.println("<tr><td>password</td><td>"+password+"</td></tr>");
out.println("</table>");*/
%>
</body>
</html>
点击数:327