Servlet+Mysql+tomcat实现登录操作

1.首先,建立一个Web项目

2.导入mysql驱动包

3.创建数据库工具类ConnectionManage

package com.servlet;
import java.sql.*;
public class ConnectionManage {
//定义数据库驱动字符串
private static final String Driver_CLASS="com.mysql.jdbc.Driver";
//定义数据库连接字符串
private static final String DATABASE_URL="jdbc:mysql://localhost:3306/manage";
//登录数据库用户名
private static final String DATABASE_USER="root";
//登录数据库密码
private static final String DATABASE_PASSWORD="123";
public static Connection getConnection(){
Connection dbConnection=null;
try {
Class.forName(Driver_CLASS);
dbConnection=DriverManager.getConnection(DATABASE_URL,
DATABASE_USER,DATABASE_PASSWORD);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

return dbConnection;
}
public static void closeConnection(Connection con){
try {
con.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void closeStatement(Statement st){
try {
st.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void closeResultSet(ResultSet rs){
try {
rs.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(getConnection());

}
}

4.创建处理登录请求servlet LoginServlet

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

private Connection con=null;
private PreparedStatement pstmt=null;
private ResultSet rs;
public LoginServlet() {
super();
}

public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


public void doDelete(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

// Put your code here
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username=request.getParameter("username");
String password=request.getParameter("password");
try {
String sql="SELECT * FROM admin WHERE username=? and password=?";
con=ConnectionManage.getConnection();
pstmt=con.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2,password);
rs=pstmt.executeQuery();
if(rs.next()){
request.getRequestDispatcher("/success.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}





}

public void init() throws ServletException {
// Put your code here
}

}

5.自动生成web.xml获取URL地址

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.servlet.LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginservlet</url-pattern>
</servlet-mapping>

</web-app>

6.创建登录界面

<%@ page language="java" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>管理员登录</title>
</head>

<body>
<form action="loginservlet" method="post">

用户名<input type="text" id="username" name="username">
密码<input type="password" id="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/muyian1/p/9236072.html