JSP 7
<form action="dologin.jsp" method="post">
用户名:<input id="username" type="text" name="username"/><br/>
密码:<input id="password" type="password" name="password"/><br/>
<%-- 验证码:<input type="text" name="inputVcode"/><img src="/WebProject_war_exploded/createCode"><br/>--%>
<input type="submit" value="注册">
</form>
使用分层实现注册。(必做)
<%@ 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>
<style type="text/css">
body {
background-color: azure;
}
form {
position: fixed;
top: 250px;
left: 650px;
background-color: #ADD8E6;
border-radius: 5px;
}
div {
font-size: 22px;
margin-top: 20px;
padding-left: 40px;
}
input {
width: 200px;
height: 20px;
border-radius: 5px;
}
#tj {
width: 80px;
height: 30px;
margin-left: 140px;
border-radius: 5px;
font-size: 16px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<form action="/school/panduanreg.jsp" method="post">
<div>
账 号: <input type="text" name="username">
</div>
<div>
密 码: <input type="text" name="password">
</div>
<div>
<input type="submit" id="tj" value="注册">
</div>
</form>
</body>
<script type="text/javascript">
</script>
</html>
<%@page import="dao.userDao"%>
<%@page import="bean.user"%>
<%@ 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>
<%
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("UTF-8");
HttpSession s = request.getSession();
String username = request.getParameter("username");
String password1 = request.getParameter("password");
user u = new user();
u.setUname(username);
u.setUpwd(password1);
userDao ud = new userDao();
int flag = ud.addUser(u);
if (flag > 0) {
out.write("注册成功");
} else {
response.sendRedirect("/school/reg.jsp");
}
%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
public int addUser(user u) throws Exception {
int i = 0;
Class.forName("com.mysql.jdbc.Driver");
// 2.提供另外三个连接的基本信息
String url = "jdbc:mysql://localhost:3306/school";
String user = "root";
String password = "root";
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement ps = conn.prepareStatement("insert into user (uname,upwd)values(?,?)");
ps.setString(1, u.getUname());
ps.setString(2, u.getUpwd());
return i = ps.executeUpdate();
}
package bean;
public class user {
private int uid;
private String uname;
private String upwd;
public user() {
super();
}
public user(int uid, String uname, String upwd) {
super();
this.uid = uid;
this.uname = uname;
this.upwd = upwd;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}

使用分层实现登录。(选做)
package com.example.bean;
public class User {
private int UserID;
private String UserName;
private String UserEmail;
private String Userphone;
private String UserZhishang;
private String Userpassword;
public User(int userID, String userName, String userEmail, String userphone, String userZhishang, String userpassword) {
UserID = userID;
UserName = userName;
UserEmail = userEmail;
Userphone = userphone;
UserZhishang = userZhishang;
Userpassword = userpassword;
}
public User(String userName, String userEmail, String userphone, String userZhishang, String userpassword) {
UserName = userName;
UserEmail = userEmail;
Userphone = userphone;
UserZhishang = userZhishang;
Userpassword = userpassword;
}
public User() {
}
public String getUserpassword() {
return Userpassword;
}
public void setUserpassword(String userpassword) {
Userpassword = userpassword;
}
public int getUserID() {
return UserID;
}
public void setUserID(int userID) {
UserID = userID;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getUserEmail() {
return UserEmail;
}
public void setUserEmail(String userEmail) {
UserEmail = userEmail;
}
public String getUserphone() {
return Userphone;
}
public void setUserphone(String userphone) {
Userphone = userphone;
}
public String getUserZhishang() {
return UserZhishang;
}
public void setUserZhishang(String userZhishang) {
UserZhishang = userZhishang;
}
@Override
public String toString() {
return "User{" +
"UserID=" + UserID +
", UserName='" + UserName + '\'' +
", UserEmail='" + UserEmail + '\'' +
", Userphone=" + Userphone +
", UserZhishang='" + UserZhishang + '\'' +
", Userpassword='" + Userpassword + '\'' +
'}';
}
}
public boolean QueryUser(String u,String p){
Connection con=null;
PreparedStatement pre=null;
try {
con= DriverManager.getConnection(url, username, password);
String sql="SELECT * FROM USER WHERE user_name=? AND user_password=?";
pre = con.prepareStatement(sql);
pre.setString(1,u);
pre.setString(2,p);
ResultSet rs = pre.executeQuery();
boolean b = rs.next();
return b;
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
try {
if (con!=null)
con.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
try {
if (pre!=null)
pre.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return false;
}
package com.example.servlet;
import com.example.Dao.ConnectionPool;
import com.example.Dao.UserDao;
import com.example.bean.User;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;
public class Login extends HttpServlet {
@Override
public void init() throws ServletException {
super.init();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=UTF-8");
String uusername = req.getParameter("username").trim();
String ppassword = req.getParameter("password").trim();
int code1Int= 0;
int code2Int= 0;
int yzmInt= 0;
if (code1Int+code2Int==yzmInt) {
try {
String yzm = req.getParameter("verify").trim();
String code1 = req.getParameter("code1").trim();
String code2 = req.getParameter("code2").trim();
code1Int = Integer.parseInt(code1);
code2Int = Integer.parseInt(code2);
yzmInt = Integer.parseInt(yzm);
UserDao userDao = new UserDao();
boolean b = userDao.QueryUser(uusername, ppassword);
if (code1Int + code2Int == yzmInt) {
if (b == true) {
HttpSession session = req.getSession();
session.setAttribute("uusername", uusername);
req.getRequestDispatcher("index.jsp").forward(req, resp);
} else {
resp.getWriter().println("<h1>登陆失败<h1>");
resp.getWriter().println("<h1>用户名或密码输入错误<h1>");
resp.getWriter().println("<h1>5s后页面跳转....</h1>");
resp.getWriter().println("<a href=\"login.jsp\">返回至登陆界面</a>");
resp.setHeader("Refresh", "5;url=login.jsp");
}
} else {
resp.getWriter().println("<h1>验证码输入错误<h1>");
resp.getWriter().println("<h1>3s后页面跳转....</h1>");
resp.getWriter().println("<a href=\"login.jsp\">返回至登陆界面</a>");
resp.setHeader("Refresh", "3;url=login.jsp");
}
} catch (NumberFormatException e) {
resp.getWriter().println("<h1>验证码为空,千万不可以为空哦<h1>");
resp.getWriter().println("<h1>3s后页面跳转....</h1>");
resp.getWriter().println("<a href=\"login.jsp\">返回至登陆界面</a>");
resp.setHeader("Refresh","3;url=login.jsp");
}
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
public void success(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}

浙公网安备 33010602011771号