Index
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>index</title>
</head>
<body>
<h2>메인페이지</h2>
<!--session이 비어있으면 로그인 페이지 띄움-->
<%if(session.getAttribute("sessionId")==null){%>
<h3>로그인을 해주세요</h3>
<ul>
<li><a href="login.jsp">로그인</a></li>
<li>회원가입</li>
<%}else{%>
<!--session이 있으면 로그아웃 페이지 띄움-->
<h3><%=session.getAttribute("sessionName") %>님 환영합니다.</h3>
<ul>
<li><a href="logout.jsp">로그아웃</a></li>
<li>회원정보수정</li>
<li>게시판</li>
<li><a href="memberAll.jsp">전체회원보기</a></li>
<%} %>
</ul>
</body>
</html>
<로그인전>

<로그인후>

login
form을 통해서 DoLogin으로 보냄 / DoLogin 파일은 java파일이다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>login</title>
<style>
div {width:400px; height:300px; border:1px solid black; margin:50px auto;
padding:30px;
}
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
function loginBtn(){
if($("#id").val().length<1){
alert("아이디를 입력해주세요");
$("#id").val("");
$("#id").focus();
return false;
}
loginfrm.submit();
}
</script>
</head>
<body>
<h2>로그인</h2>
<div>
<form action="DoLogin" method="post" name="loginfrm">
<label>아이디</label>
<input type="text" name="id" id="id"><br>
<label>패스워드</label>
<input type="password" name="pw" id="pw"><br>
<input type="button" value="로그인" onclick="loginBtn()" ><br>
</form>
</div>
</body>
</html>
DoLogin
1.Get or Post 중 하나의 방식으로 정보를 받아 올텐데,
둘 중 어느 것으로 정보를 받아와도,
각자의 method 안에 doAction(request, response)를 넣어줌으로써,
doAction method가 실행된다.
2.doAction method에서는
package com.java.www;
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;
import javax.servlet.http.HttpSession;
@WebServlet("/DoLogin")
public class DoLogin extends HttpServlet {
protected void doAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doAction");
request.setCharacterEncoding("utf-8"); //한국어로 인코딩하기
String id=request.getParameter("id"); //form에서 타고온 id정보 받기
String pw=request.getParameter("pw"); //form에서 타고온 pw정보 받기
MemberDao mdao=new MemberDao(); //db연동 코드 객체
MemberDto mdto=mdao.selectLogin(id,pw);//회원객체를 담기
if(mdto.getId()==null) {
response.setContentType("text/html; charset=utf-8");// 보여지는거 한글로 처리
PrintWriter writer=response.getWriter(); //htmltag output하기위한거 servlet
writer.println("<html><head></head><body>");
writer.println("<script>");
writer.println("alert('아이디 또는 패스워드가 일치하지 않습니다. 다시 로그인해주세요');");
writer.println("location.href='login.jsp';");
writer.println("</script>");
writer.println("</body></html>");
}else {
HttpSession session=request.getSession();
session.setAttribute("sessionId", id);
session.setAttribute("sessionName", mdto.getName());
response.setContentType("text/html; charset=utf-8");
PrintWriter writer=response.getWriter();
writer.println("<html><head></head><body>");
writer.println("<script>");
writer.println("alert('로그인 성공');");
writer.println("location.href='index.jsp';");
writer.println("</script>");
writer.println("</body></html>");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
doAction(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost");
doAction(request, response);
}
}