JSP_tomcat_mysql_注冊验证用户;

本文出自:http://blog.csdn.net/svitter

资源下载:

github:

git clone https://github.com/Svtter/JSP-tomcat-mysql

我把注冊和登陆写在一个Servlet处理里面了。一个用get,一个用post,这是不对的。

以后有时间改正吧。。假设单纯为了作业能够提交这个。

get和post简单来说差别就在于post更加安全。

能够详细查一查。。

使用了servlet:

web.xml:

<?

xml version="1.0" encoding="UTF-8"?

>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>JSP_servlet_javabean</display-name>
<servlet>
<description>配置用於用戶登錄的Servlet</description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
</web-app>

四个java文件:

LoginServlet.java://用于验证:

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import bean.ConnectionBean;
import bean.userBean; /**
* @author Svitter
*
*/
@SuppressWarnings("serial")
public class LoginServlet extends HttpServlet {
userBean user = new userBean();
ConnectionBean connBean = new ConnectionBean();
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String checkLogin;
String username = request.getParameter("login");
String password = request.getParameter("password");
checkLogin = connBean.checkUser(username, password);
if(checkLogin.equals("Success")) {
user.setUserName(username);
request.setAttribute("user", user);
getServletConfig().getServletContext().getRequestDispatcher("/loginsuccess.jsp").forward(request, response);
} else {
request.setAttribute("checkLogin", checkLogin);
getServletConfig().getServletContext().getRequestDispatcher("/loginCheck.jsp").forward(request, response);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
String username = request.getParameter("login");
String password = request.getParameter("password");
connBean.addUser(username, password);
getServletConfig().getServletContext().getRequestDispatcher("/login.html").forward(request, response);
}
}

bean.GetConnection.java://用于连接数据库。数据库配置文件property.conf

package bean;
import java.io.*;
import java.util.Properties;
import java.sql.*;
public class GetConnection {
private static Properties p;
static{
try{
p = new Properties();
InputStream is = GetConnection.class.getResourceAsStream("property.conf");
p.load(is);
is.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public static String getProperty(String key){
return p.getProperty(key);
}
static String driver = GetConnection.getProperty("driver");
static String url = GetConnection.getProperty("url");
static String name = GetConnection.getProperty("user");
static String pass = GetConnection.getProperty("password");
static{
try{
Class.forName(driver);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, name, pass);
}
}

property.conf:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/users
user=root
password=root

bean.ConnectionBean.java://用于数据库操作

package bean;

import java.sql.*;

public class ConnectionBean {
private Connection con;
public ConnectionBean() {
try{
con = GetConnection.getConnection();
} catch (Exception e){
e.printStackTrace();
}
}
//向数据库加入信息
public boolean addUser(String name, String password){
try{
PreparedStatement pstmt = con.prepareStatement("insert into ur(name, passwd) values(? ,? )");
pstmt.setString(1, name);
pstmt.setString(2, password);
pstmt.execute();
return true;
} catch (Exception e){
e.printStackTrace();
return false;
}
}
public ResultSet getUser(String name){
try {
Statement stm = con.createStatement();
ResultSet result = stm.executeQuery("select * from ur where name='"+name+"'");
return result;
} catch (Exception e){
e.printStackTrace();
return null;
}
}
public String checkUser(String name, String passwd){
String checkUser=null;
try{
Statement stm = con.createStatement();
ResultSet result = stm.executeQuery("select * from ur where name='"+name+"'");
if(result.next()==false){
checkUser = "No user";
}
else{
if(result.getString("passwd").equals(passwd)){
checkUser = "Success";
}
else{
checkUser = "Wrong passwd";
}
}
}catch (Exception e){
e.printStackTrace();
}
return checkUser;
}
}
上一篇:浅析Android Dialog中setContentView()方法


下一篇:post 传递参数中包含 html 代码解决办法,js加密,.net解密