JSP Web Programming : model2 방식
LoginForm.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <body> <div style="text-align: center"> <h2>로그인</h2> <!--web.xml을 만들고 그 안에 서블릿을 등록하여 서블릿을 인식하도록 하거나 어노테이션을 사용하여 jsp가 자바코드(서블릿)를 바로 호출하게할 수 있다.--> <!-- 서블릿파일에 .java보다는 .do를 많이 붙인다. 파일 확장자가 아니기때문에 dot 빼도 상관없다. LoginProc.do는 파일명이 아니라 참조변수 쯤으로 생각하면 된다. 자바파일의 클래스명과는 달라도되지만 어노테이션에 들어가는 이름과는 같아야한다. --> <form action="LoginProc.do" method="post"> <table width="300" border="1" align="center" > <tr height="40"> <td width="120">아이디</td> <td width="180"> <input type="text" name="id"></td> </tr> <tr height="40"> <td width="120">패스워드</td> <td width="180"> <input type="password" name="password"></td> </tr> <tr height="40"> <td align="center" colspan="2"> <input type="submit" value ="로그인"> </td> </tr> </table> </form> </div> </body> </html> | cs |
LoginProc.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | package control; 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; @WebServlet("/LoginProc.do") //웹서버가 서블릿을 인식하게해주는 어노테이션 //.do까지 붙여줘야한다. public class LoginProc extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //response.getWriter().append("Served at: ").append(request.getContextPath()); reqPro(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { reqPro(request, response); } // 위 doPost메서드와 이름과 접근제한자 외에 똑같이 만들어주고 // 위 두 방식 모두 reqPro를 호출하게끔 한다. = 모두 reqPro가 처리하도록한다. //자바파일은 HttpServlet을 상속해야만 goGet과 doPost기능을 쓸 수 있다. //이러한 클래스를 '서블릿 클래스'라고한다. public void reqPro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String password = request.getParameter("password"); System.out.println("id=" +id); //request객체에 데이터를 저장 //session에 저장해도된다. request.setAttribute("id", id); request.setAttribute("password", password); RequestDispatcher dis = request.getRequestDispatcher("LoginProc.jsp"); dis.forward(request, response); } } | cs |
1. Servlet 파일에서 reqPro 메서드를 만들어 get방식과 post방식 모두 reqPro가 처리하도록 만든다.
2. reqPro 메서드에서 request.getParameter로 form에서 받아온 정보를 변수에 저장하고
3. request.setAttribute로 이름과 오브젝트(데이터 저장한 변수)를 설정한다.
4. RequestDispatcher dis = request.getRequestDispatcher("데이터 넘길 jsp페이지")
5. dis.forward에 매개변수로 받아온 request와 response를 매개값으로 준다.
LoginProc.jsp
1 2 3 4 5 6 7 8 9 10 | <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <body> 넘어온 데이터는 ${id }와 ${password } </body> </html> | cs |
'JSP > JSP & Servlet ' 카테고리의 다른 글
JSTL (Java Standard Tag Library)- 1 (0) | 2019.05.21 |
---|---|
EL(Expression Language) 사용법 -1 (0) | 2019.05.21 |
파일 업로드 (0) | 2019.05.21 |
(JSP 인강) jsp web MVC model2 programming (0) | 2019.05.17 |
JSP 게시판 - 전체게시글보기 카운트 (0) | 2019.05.14 |