--> -->

JSP

class SaveUserServlet

package com.slipp.user;

import java.io.IOException;

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 com.slipp.db.Database;

@WebServlet("/users/save")
public class SaveUserServlet extends HttpServlet {

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String userId=req.getParameter("userId");
		String password=req.getParameter("password");
		String name=req.getParameter("name");
		String email=req.getParameter("email");
	
		User user=new User(userId, password, name, email);

		//Database.addUser(user);
		UserDAO dao =new UserDAO();
		dao.insert(user);
		
		
		resp.sendRedirect("/");
		
	}
	
	

}

 

 

 

 

class LoginServlet

package com.slipp.user;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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;

import com.slipp.PasswordMismatchException;
import com.slipp.db.Database;


@WebServlet("/users/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userId=request.getParameter("userId");
		String password=request.getParameter("password");
		UserDAO dao =new UserDAO();
	
		//User user=Database.findByUserId(userId);
		//User user=dao.findByUserId(userId);
		HttpSession session=request.getSession();
		
		try{
			User.login(userId, password);
			session.setAttribute("userId", userId);
			
			response.sendRedirect("/");
		}catch(NotFoundUserException e){
			forwardJSP(request, response, "존재하지 않는 사용자 입니다. 다시 로그인하세요.");
		}catch(PasswordMismatchException e){
			forwardJSP(request, response, "비밀번호가 틀립니다. 다시 로그인하세요.");
		}

	}


	private void forwardJSP(HttpServletRequest request, HttpServletResponse response, String errorMessage)
			throws ServletException, IOException {
		request.setAttribute("errorMessage", errorMessage);
		RequestDispatcher rd=request.getRequestDispatcher("login.jsp");
		rd.forward(request, response);
	}


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

		doGet(request, response);
	}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

중복된 곤란은 승리의 기회다. -처칠

댓글 ( 0)

댓글 남기기

작성