您的位置:首页 > 其它

简单登入模块

2015-11-28 17:40 302 查看
主类

package Servlet;

import java.sql.*;
public class Dao {
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static Connection getconn() throws SQLException {
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8", "root", "root");
return conn;
}
public static void closeconn(Connection conn) throws SQLException {
if (conn != null && !conn.isClosed()) {
conn.close();
}
}

}
数据插入

package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
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;

@SuppressWarnings("serial")
public class Input extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("gb2312");
String userName = request.getParameter("userName");
String userPassword = request.getParameter("userPassword");
String sql = "insert into user values(?,?,?)";
PrintWriter out = response.getWriter();
// HttpSession session = request.getSession();
Connection conn = null;
try {
conn = Dao.getconn();
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
if (conn != null) {
PreparedStatement ps;
ps =conn.prepareStatement(sql);
ps.setInt(1, 0);
ps.setString(2, userName);
ps.setString(3, userPassword);
int rs = ps.executeUpdate();
if (rs != 0) {
response.sendRedirect("Students.jsp");
} else {
out.println("添加失败!");
}
Dao.closeconn(conn);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
数据导出

package 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 javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class Output extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setCharacterEncoding("gb2312");
String UserName = request.getParameter("UserName");
String UserPassword = request.getParameter("UserPassword");
String sql = "select * from user where  UserName=? and UserPassword=?";
PrintWriter out = response.getWriter();
//HttpSession session = request.getSession();
Connection conn = null;
try {
conn = Dao.getconn();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (conn != null) {
PreparedStatement ps;
try {
ps =conn.prepareStatement(sql);
ps.setString(1, UserName);
ps.setString(2, UserPassword);
ResultSet rs = ps.executeQuery();
//如果有数据,则跳转,如果没有下一条数据(空结果集)则 向网页输出登入失败
if(rs.next()){
response.sendRedirect("Students.jsp");
}else
out.println("登入失败!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Dao.closeconn(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
登入界面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>登入</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
<center>
<h1>系统登入</h1>
<form action="Output" method="post">
用户名:<input type="text" name="UserName">
<p>
密 码:<input type="password" name="UserPassword">
<p>
<input type="submit" value="提交"> <input type="reset"
value="取消">
<p>
還沒註冊?<a href="index.jsp">點擊</a>註冊
</form>
</center>
</body>
</html>
注册界面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>登入</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
<center>
<form action="Input" method="post">
<h1>用户注册</h1>
用户名:<input type="text" name="userName">
<p>
密 码:<input type="password" name="userPassword">
<p>
<input type="submit" value="注册"> <input type="reset"
value="取消"><br>
</form>
</center>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: