java后台代码:
package util;
/**
* @author LYB
* 实现获取连接数据库对象方法类
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
//获取连接数据库对象
public class DBUtil {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(String url){
Connection conn = null;
Properties pro = null;
InputStream in = null;
try {
pro = new Properties();
in = new FileInputStream("e:"+File.separator+"info.properties");
pro.load(in);
conn = DriverManager.getConnection(url,pro);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
package myservlet;
/**
* @author LYB
* java后台实现登录servlet代码
*/
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.DBUtil;
public class LoginServletDemo extends HttpServlet{
boolean flag = true;
@Override
public void init() throws ServletException {
System.out.println("LoginServlet初始化了...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name = req.getParameter("username");
String pwd = req.getParameter("pwd");
login(name,pwd);
if(flag){
//resp.sendRedirect("/WebTest/MyJsp/welcome.jsp");
req.getRequestDispatcher("/MyJsp/welcome.jsp").forward(req, resp);
}else{
//resp.sendRedirect("/WebTest/MyHtml/fail.html");
req.getRequestDispatcher("/MyHtml/fali.html").forward(req, resp);
}
}
//用户登录方法
private void login(String name, String pwd) {
Connection conn = DBUtil.getConnection("jdbc:mysql://localhost:3306/mydb");
PreparedStatement ps = null;
ResultSet rs = null;
String sql = "select * from user where name=? and pwd=?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, pwd);
rs = ps.executeQuery();
if(rs.next()){
System.out.println("该用户存在...");
}else{
System.out.println("该用户没有注册我们的网站...");
flag = false;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}
package myservlet;
/**
* @author LYB
* 实现注册Servlet代码
*/
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import util.DBUtil;
//用户登录servlet类
public class RegisterServletDemo extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println("RegisterServlet初始化了...");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name = req.getParameter("username");
String pwd = req.getParameter("pwd");
register(name, pwd);
req.getRequestDispatcher("/MyJsp/success.jsp").forward(req, resp);
}
// 用户注册方法
private void register(String name,String pwd) {
Connection conn = DBUtil.getConnection("jdbc:mysql://localhost:3306/mydb");
PreparedStatement ps = null;
String sql = "insert into user(name,pwd)values(?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, pwd);
ps.executeUpdate();
System.out.println("数据库更新成功...");
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
ps.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}
登录界面
欢迎光临
注册界面
欢迎光临
<%@page import="java.net.URLDecoder"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
注册成功!
<%String username=request.getParameter("username");%>
恭喜,<%=username%>注册成功!
<%@page import="java.net.URLDecoder"%>
<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%
String username=request.getParameter("username"); // 获取请求参数名为user的参数值
%>
欢迎<%=username%>,你是我们的VIP用户!
失败页面
用户名或密码错误,请重新登录!
点击跳转
服务器配置部署符web.xml:
WebTest
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
firstServlet
myservlet.LoginServletDemo
0
secondServlet
myservlet.RegisterServletDemo
1
firstServlet
/MyHtml/loginURL
secondServlet
/MyHtml/registerURL