<%@ 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>reg</title>
</head>
<body>
${requestScope.msg}
<form action="regSverlet" method="post">
<input type="text" name="uname"/></br>
<input type="password" name="upass"/></br>
<input type="submit" value="reg"/>
</form>
</body>
</html>
登录页面:
<%@ 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>reg</title>
</head>
<body>
${requestScope.msg}
<form action="regSverlet" method="post">
<input type="text" name="uname"/></br>
<input type="password" name="upass"/></br>
<input type="submit" value="reg"/>
</form>
</body>
</html>
注册逻辑:
@WebServlet("/regSverlet")
public class RegServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取参数
String name=req.getParameter("uname");
String pass=req.getParameter("upass");
//判断用户注册名和密码是否为空
if(name==null || name.equals("")){
req.setAttribute("msg", " name not null");
req.getRequestDispatcher("regsiter.jsp").forward(req, resp);
}
else if(pass==null || pass.equals("")){
req.setAttribute("msg", "pass not null");
req.getRequestDispatcher("regsiter.jsp").forward(req, resp);
}else{
//满足条件
System.out.printf(name,pass);
Connection conn=null;
PreparedStatement ps=null;
try {
//加载数据库驱动
Class.forName("org.gjt.mm.mysql.Driver");
String url="jdbc:mysql://localhost:3306/mydb1?characterEncoding=utf8";
conn = DriverManager.getConnection(url,"root","123456");
ps = conn.prepareStatement("insert into user(name,pass)values(?,?)");
ps.setString(1, name);
ps.setString(2, pass);
int m = ps.executeUpdate();
System.out.println("本次成功注册"+m+"个用户");
req.getRequestDispatcher("login.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
登录逻辑:
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
public void init(ServletConfig config) throws ServletException {
//User us=new User();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取参数
String uname=req.getParameter("username");
String upass=req.getParameter("password");
//非空验证
if(uname==null || uname.equals("")){
req.setAttribute("error", " name not null");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
else if(upass==null || upass.equals("")){
req.setAttribute("error", "pass not null");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}else{
//条件满足
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
try {
//加载数据库驱动
Class.forName("org.gjt.mm.mysql.Driver");
//获取链接
String url="jdbc:mysql://localhost:3306/mydb1?characterEncoding=utf8";
conn = DriverManager.getConnection(url,"root","123456");
//查找数据库
ps = conn.prepareStatement("select name,pass from user where=? and pass=?");
rs = ps.executeQuery();
if(rs!=null || rs>0){
req.getSession().setAttribute("suc", name);
req.getRequestDispatcher("/success/success.jsp").forward(req, resp);
}
else{
//不满足条件
req.setAttribute("error", "your pass or user error");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
/*
while(rs.next()){
String name=rs.getString("name");String pass=rs.getString("pass");
//判断密码是否相同
if(name.equals(uname) || name==uname && upass==pass || upass.equals(pass)){
req.getSession().setAttribute("suc", name);
req.getRequestDispatcher("/success/success.jsp").forward(req, resp);
}else{
//不满足条件
req.setAttribute("error", "your pass or user error");
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
}
*/
} catch (Exception e) {e.printStackTrace();
}finally {
try {
//关闭数据库
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
过滤器:
@WebFilter(filterName="FilterLogin",urlPatterns={"/success/*"})
public class FilterLogin implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest) arg0;
String name =(String)req.getSession().getAttribute("suc");
if(name!=null){
arg2.doFilter(arg0, arg1);
}else{
req.setAttribute("error", "you not login");
req.getRequestDispatcher("login.jsp").forward(arg0, arg1);
}
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
package com.phone.jdbc;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
@WebFilter(filterName="LogFilter",urlPatterns={"/*"},initParams={
@WebInitParam(name="finaName",value="log.txt")
})
public class LogFilter implements Filter {
private PrintWriter pw;
@Override
public void destroy() {
// TODO Auto-generated method stub
pw.close();
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
throws IOException, ServletException {
HttpServletRequest req=(HttpServletRequest)arg0;
String url = req.getRequestURI();
String at = (String)req.getSession().getAttribute("suc");
pw.println("用户"+at+new Date()+"时间访问"+url);
pw.flush();
arg2.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig con) throws ServletException {
String path = con.getServletContext().getRealPath("/");
String txt = con.getInitParameter("finaName");
try {
pw=new PrintWriter(new File(path, txt));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}