본문 바로가기

콩's EDUCATION/콩's JSP&SERVLET

간단한 로그인 만들기

ex01 (로그인 페이지)


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">

<title>Login Form</title>

<style>

table,tr {margin:1px;padding:1px;border:1px solid gray;}

td {align:center;}

</style>

</head>

<body>

<form action="login" method="post">

<table>

<tr><td colspan="2">로그인</td></tr>

<tr><td>아이디</td><td><input type="text" name="userid" id="userid"></td></tr>

<tr><td>패스워드</td><td><input type="password" name="userpwd" id="userpwd"></td></tr>

<tr><td colspan="2"><input type="submit" value="로그인">

<input type="reset" value="취소"></td></tr><tr></tr><tr></tr>

</table>

</body>

</html>


login success 페이지


<%@ page contentType="text/html; charset=utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>로그인 성공</title>

<style type="text/css">

body{color:blue;}

</style>

</head>

<body>

로그인 성공했습니다.<br>

환영합니다 <br>

</body>

</html>


login fail 페이지


<%@ page contentType="text/html; charset=utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>로그인 실패</title>

<style type="text/css">

body{color:red;}

</style>

</head>

<body>

로그인 실패했습니다.<br>

다시 접속해주세요<br>

</body>

</html>


login servlet 페이지


package com.web3.login;


import java.io.IOException;

import java.io.PrintWriter;


import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


/**

 * Servlet implementation class LoginServlet

 */

@WebServlet("/login")

public class LoginServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

       

    /**

     * @see HttpServlet#HttpServlet()

     */

    public LoginServlet() {

        super();

    }


/**

* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

*/

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

// 요청 처리후 응답을 위한 마임타입과 문자셋 설정

response.setContentType("text/html;charset=utf-8");

// 응답 결과를 클라이언트에게 보내기 위해서 문자스트림을 생성

PrintWriter out = response.getWriter();

// 파라미터를 받아오기

String uid = request.getParameter("userid");

String pwd = request.getParameter("userpwd");

// DB에 Access 해서 userid, password 일치 여부 체크

if(uid.equals("admin")&& pwd.equals("a1234"))

{

response.sendRedirect("./loginSuccess.jsp");

}else{

response.sendRedirect("./loginFail.jsp");

}

}


}


구조


servlet 구조는 getPost method 사용.