You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.3 KiB
73 lines
2.3 KiB
package WeChat;
|
|
|
|
import java.io.IOException;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
import javax.servlet.ServletConfig;
|
|
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;
|
|
|
|
// 连接数据库的我们是服务开始的时候
|
|
@WebServlet(name = "LoginDatabase", urlPatterns = {"/Startup.do"}, loadOnStartup = 1)
|
|
public class LoginDatabase extends HttpServlet {
|
|
private String mysql, url, user, passwd;
|
|
private Connection conn;
|
|
private Statement state;
|
|
private void initDataBase() throws ClassNotFoundException, SQLException {
|
|
Class.forName(mysql);
|
|
conn = DriverManager.getConnection(url,user,passwd);
|
|
state = conn.createStatement();
|
|
}
|
|
private void closeDataBase(){
|
|
if(state != null) {
|
|
try{
|
|
state.close();
|
|
}catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
if(conn != null) {
|
|
try{
|
|
conn.close();
|
|
}catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
public void init(ServletConfig config) throws ServletException{
|
|
super.init(config);
|
|
ServletContext context=getServletContext();
|
|
mysql = context.getInitParameter("mysql");
|
|
url = context.getInitParameter("url");
|
|
user = context.getInitParameter("user");
|
|
passwd = context.getInitParameter("passwd");
|
|
try{
|
|
initDataBase();
|
|
}catch (Exception e){
|
|
e.printStackTrace();
|
|
}
|
|
context.setAttribute("conn",conn);
|
|
context.setAttribute("state",state);
|
|
}
|
|
public void destroy(){
|
|
closeDataBase();
|
|
super.destroy();
|
|
}
|
|
public void doGet(HttpServletRequest request,
|
|
HttpServletResponse response)
|
|
throws ServletException, IOException {
|
|
response.sendRedirect("/index.html");
|
|
}
|
|
public void doPost(HttpServletRequest request,
|
|
HttpServletResponse response)
|
|
throws ServletException, IOException {
|
|
doGet(request, response);
|
|
}
|
|
}
|