JSP도 곧 서블릿임을 잊지말자.
GET 방식 : http://naver.com?이름=shkim&암호=1234
get방식으로 한글을 보낼경우 글이 깨지는데 이때는 server을 열고 server.xml을 열자. 65라인에 URIEncoding=”utf-8”을 추가해주자
WebContent에 form.jsp 생성 form , 절대경로, 상대경로에 대해 알아보자
form.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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="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>Insert title here</title> </head> <body> <!-- h1태그는 블록 태그라 아래로 내려감 --> <h1>폼테스트</h1> <h2>폼테스트</h2> <h3>폼테스트</h3> <h4>폼테스트</h4> <h5>폼테스트</h5> <!-- 상대경로(form.jsp의 위치부터) --> <img src = "images/java.png" > <!-- 절대경로 --> <a href = "/helloweb/index.jsp" >메인으로 가기</a> <!-- 절대경로를 처리하는 안정적인 방법 --> <a href = "<%=request.getContextPath() %>/index.jsp" >메인으로 가기</a> <!-- form 태그는 디폴트로 겟방식으로 날린다. --> <form method="post" action="<%= request.getContextPath() %>/join" > <!-- input 태그는 인라인 태그라 옆으로간다. --> <input type="hidden" name="type" value="나쁜놈" /> 이메일: <input type="text" name="email" value="" > <br><br> 비밀번호: <input type="password" name="password" value="" > <br><br> 이름: <input type="text" name="name" > <br><br> 성별: <!-- radio는 name속성을 같게 해줘야 하나만 선택된다. --> <input type="radio" name="gender" value="female" /> 여 <input type="radio" name="gender" value="male checked=checked" /> 남 <br><br> 생년: <select name = "birth-year" > <option value="1995" >1995 </option> <option value="1994" >1994 </option> <option value="1993" >1993 </option> <option value="1992" >1992 </option> <option value="1991" >1991 </option> <option value="1990" selected>1990 </option> </select> <br><br> 취미: <input type="checkbox" name="hobby" value="reading" />독서 <input type="checkbox" name="hobby" value="sleeping" />잠자기 <input type="checkbox" name="hobby" value="swimming" />수영 <input type="checkbox" name="hobby" value="coding" />코딩 <br><br> 자기소개: <br><br> <textarea rows="15" cols="80" name="self-intro" >1234 1234 1234 </textarea> <br><br> <input type="submit" value="가입" > <br><br> </form> </body> </html>
Java Resource에 서블릿 파일 생성
join.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 55 56 57 58 59 60 61 62 63 64 65 package com.bigdata2017.helloweb.servlet;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;@WebServlet("/join") public class join extends HttpServlet { private static final long serialVersionUID = 1L ; protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding( "utf-8" ); String type = request.getParameter("type" ); System.out.println(type); String email = request.getParameter("email" ); System.out.println(email); String password = request.getParameter("password" ); System.out.println(password); String name = request.getParameter("name" ); System.out.println(name); String gender = request.getParameter("gender" ); System.out.println(gender); String birthYear = request.getParameter("birth-year" ); System.out.println(birthYear); String hobbies[] = request.getParameterValues( "hobby" ); if ( hobbies != null ) { for ( String hobby : hobbies) { System.out.println( hobby ); } } String selfIntro = request.getParameter("self-intro" ); System.out.println(selfIntro); response.setContentType( "text/html; charset=utf-8" ); PrintWriter out = response.getWriter(); out.println("<h1>성공적으로 가입되었습니다</h1>" ); out.print("<a href='/helloweb/form.jsp'>폼으로가기</a>" ); } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }