验证中...
Snippet 1 Snippet 2 Snippet 3 Snippet 4 Snippet 5 Snippet 6 Snippet 7 Snippet 8 Snippet 9
index.jsp
Raw Copy
<%@page import="com.register.model.User"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>主页</title>
</head>
<body>
<%
Object o=session.getAttribute("user");
if(o!=null){
User user=(User)o;
out.println("当前用户名为"+user.getUsername()+"<br/>");
}
Object o2=application.getAttribute("usernumber");
if(o2!=null){
out.println("当前登录人数为"+o2+"<br/>");
}
%>
<a href="login.jsp">登录</a> <a href="register.jsp">注册</a>
</body>
</html>
login.jsp
Raw Copy
<%@page import="com.register.model.User" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登陆界面</title>
</head>
<body>
<%
Object o=session.getAttribute("user");
if(o!=null){
User user=(User)o;
out.println("当前用户名为"+user.getUsername());
}
%>
<%
Object msg=request.getAttribute("msg");
if(msg!=null)out.println(msg);
%>
<%
Object success=request.getAttribute("success");
if(success!=null)out.println(success);
%>
<form action="LoginServlet" method="post" >
昵称<input type="text" name="username"><br/>
密码<input type="password" name="password"><br/>
<input type="submit" value="登录">
</form>
<%
Object key=request.getAttribute("key");
if(key!=null)out.println(key);
%>
</body>
</html>
personCenter.jsp
Raw Copy
<%@page import="com.register.util.DButil"%>
<%@page import="com.register.model.User" %>
<%@ 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>
个人中心<br/>
<%
User user=(User)request.getAttribute("user");
String username=user.getUsername();
String phone=user.getPhone();
out.println("用户名为:"+username);
out.println("电话是"+phone);
%>
<a href="index.jsp">回退</a>
</body>
</html>
register.jsp
Raw Copy
<%@page import="com.register.model.User" %>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>注册</title>
</head>
<body>
<%
Object o=request.getAttribute("msg");
if(o!=null){
out.println(o);
}
Object o2=session.getAttribute("user");
if(o2!=null){
User user=(User)o2;
out.println("当前用户名为"+user.getUsername());
}
%>
<form action="RegisterServlet" method="post" >
昵称<input type="text" name="username"><br/>
手机号<input type="text" name="phone"><br/>
密码<input type="password" name="password"><br/>
重复密码<input type="password" name="password_r"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>
User.java
Raw Copy
package com.register.model;
public class User {
private String username;
private String phone;
private String password;
public User(String username, String phone, String password) {
super();
this.username = username;
this.phone = phone;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
LoginServlet.java
Raw Copy
package com.register.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.register.model.User;
import com.register.util.DButil;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
User user=DButil.verifyAccount(username, password);
int number=0;//统计登录网站的人数
if(user==null){//登录失败
request.setAttribute("key", "用户名或密码错误请重试");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
else{//登录成功
ServletContext application=this.getServletContext();
if(application.getAttribute("usernumber")!=null){
number=(int)application.getAttribute("usernumber");
}
number++;
application.setAttribute("usernumber", number);
request.getSession().setAttribute("user", user);
request.setAttribute("user", user);
request.getRequestDispatcher("personCenter.jsp").forward(request, response);
}
}
}
RegisterServlet.java
Raw Copy
package com.register.servlet;
import com.register.util.*;
import java.io.IOException;
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 RegisterServlet
*/
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
String phone=request.getParameter("phone");
String password=request.getParameter("password");
String password_r=request.getParameter("password_r");
int isSuccess=DButil.addUser(username, phone, password);
if(password.equals(password_r)){
request.setAttribute("success", isSuccess);
request.setAttribute("msg", "注册成功请登录");
//转发请求
request.getRequestDispatcher("login.jsp").forward(request, response);
}
else{
request.setAttribute("msg", "用户名重复请重试");
//重定向 无法传输数据
//response.sendRedirect("register.jsp");
request.getRequestDispatcher("register.jsp").forward(request, response);
}
}
}
DButil.java
Raw Copy
package com.register.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.register.model.User;
public class DButil {
// private static Map<String, User> db=new HashMap<String, User>();
// public static Boolean addUser(String username,String phone,String password) {
// if(db.containsKey(username)) {
// return false;
// }
// else {
// User user=new User(username, phone, password);
// db.put(username, user);
// return true;
// }
//
//// }
// public static User verifyAccount(String username,String password) {
// if(db.containsKey(username)) {
// User user=db.get(username);
// if(user.getPassword().equals(password)) {
// return user;
// }else {
// return null;
// }
// }
// return null;
// }
public static User verifyAccount(String username,String password) {
Connection connection=null;
Statement statement=null;
ResultSet result=null;
try {
connection= SqlUtil.getConnection();
String sql="select * from user where username= ? and password= ?";
PreparedStatement preparedStatement=connection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
result=preparedStatement.executeQuery();
if(result.next()) {
String username_t=result.getString("username");
String phone=result.getString("phone");
String password_t=result.getString("password");
User user=new User(username_t,phone,password_t);
return user;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
SqlUtil.close(connection, statement, result);
}
return null;
}
public static int addUser(String username,String phone,String password) {
int success=0;
Connection connection=null;
try {
connection= SqlUtil.getConnection();
if(connection==null)System.out.println("connection=null");
String sqlString="insert into user(username,phone,password) values(?,?,?)";
PreparedStatement preparedStatement=connection.prepareStatement(sqlString);
preparedStatement.setString(1, username);
preparedStatement.setString(2, phone);
preparedStatement.setString(3, password);
success=preparedStatement.executeUpdate();
System.out.println(success);
// if(success==1)
// return true;
// else
// return false;
} catch (Exception e) {
e.printStackTrace();
}
// finally {
// SqlUtil.close(connection, statement, result);
// }
return success;
}
}
SqlUtil.java
Raw Copy
package com.register.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlUtil {
public static final String url="jdbc:mysql://localhost:3306/web?userUnicode=true&characterEncoding=UTF8&serverTimezone=GMT";
public static final String user="root";
public static final String password="dxp19990319";
public static Connection getConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");//使用驱动连接MySQL
return DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO: handle exception
}
System.out.println("未成功创建conn");
return null;
}
public static boolean close(Connection connection,Statement statement,ResultSet result) {
try {
if(result!=null) result.close();
} catch (SQLException e) {
e.printStackTrace();
return false;
}
try {
if(statement!=null)statement.close();
} catch (SQLException e2) {
e2.printStackTrace();
return false;
}
try {
if(connection!=null)connection.close();
} catch (SQLException e3) {
e3.printStackTrace();
return false;
}
return true;
}
}

Comment list( 0 )

You need to Sign in for post a comment

Help Search