JDBC登錄

LoginCheckServlet.java

package chap05svlt;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
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;

import org.apache.commons.codec.digest.DigestUtils;

public class LoginCheckServlet extends HttpServlet {

	private void loadJDBCDriver() throws ServletException {
		try {
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
			throw new ServletException(e);
		}
	}

	private Connection getConnection() throws ServletException {
		try {
			return( DriverManager.getConnection(
					"jdbc:postgresql://localhost:5432/BookStore", "okada",
					"okada") );
		} catch (SQLException e) {
			throw new ServletException(e);
		}
	}

	private void closeConnection(Connection con) {
		try {
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	private static String getStringDigest( String inString ){
		return( DigestUtils.md5Hex( inString + "digested" ) );
	}
	
	private void forward( String dest,
						    HttpServletRequest	request,
						    HttpServletResponse	response)
						throws ServletException, IOException {
		getServletContext().getRequestDispatcher(dest).forward(request, response);
	}

	public void init() throws ServletException {
		loadJDBCDriver();
	}

	protected void doPost( HttpServletRequest	request,
							 HttpServletResponse response)
						throws ServletException, IOException {

		String account = request.getParameter("account");
		String password = request.getParameter("password");

		String password_db;
		String email;
		String name;

		Connection con = getConnection();
		try {
			PreparedStatement ps = con.prepareStatement(
					"select * from t_customer where uid like ?;");
			ps.setString(1, account);
			
			ResultSet rs = ps.executeQuery();
			if (!rs.next()) {
				throw new ServletException("no account");
			}

			password_db = rs.getString("passwordmd5");
			email = rs.getString("email");
			name = rs.getString("name");
			
			if (rs.next()) {
				throw new ServletException("too many accounts found");
			}
			rs.close();
			ps.close();

		} catch (SQLException e) {
			throw new ServletException(e);
		} finally {
			closeConnection(con);
		}

		if (password_db.equals( getStringDigest(password) )) {
			request.setAttribute("email", email);
			request.setAttribute("name", name);
			forward("/result.jsp", request, response);			
		} else {
			forward("/index.html", request, response);
		}
	}
}

index.html

<html>
<head></head>
<body>

ユーザID、パスワードを入力してください。<br>
<br>

<form action="/Chap05Svlt/LoginCheckServlet" method="POST">
	ユーザID: <input type="text" name="account">		<br>
	パスワード: <input type="password" name="password">	<br>
	
	<input type="submit" value="submit">
</form>

</body>
</html>

result.jsp

<%@ page language="java" contentType="text/html;charset=Windows-31J" %>
<html>
<head>
	<title></title>
</head>
<body>
あなたは、 <br>
氏名: <%= request.getAttribute( "name" ) %> <br>
アカウント: <%= request.getParameter( "account" ) %><br>
E-Mail: <%= request.getAttribute( "email" ) %> <br>

<a href="/Chap05Svlt/index.html">トップ</a>へ戻る。
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Chap05Svlt</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>LoginCheckServlet</display-name>
    <servlet-name>LoginCheckServlet</servlet-name>
    <servlet-class>chap05svlt.LoginCheckServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginCheckServlet</servlet-name>
    <url-pattern>/LoginCheckServlet</url-pattern>
  </servlet-mapping>
</web-app>

所需類包:

commons-codec-1.3.jar

postgresql-8.3-603.jdbc3.jar

代碼來自日本的技術圖書http://www.shuwasystem.co.jp/products/7980html/2197.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章