Undergoing

JSP/Servlet 기본 숙지 Note 본문

개발/Web Development

JSP/Servlet 기본 숙지 Note

Halkrine 2012. 5. 8. 16:23

- 한글 수식 포함하려면 <META http-equiv="Content-Type"/ content = "text/html;charset=euc-kr"> 코드 필수. 

- HTML 내에 코딩하려면 <% [coding] %>

- Servlet 작성은 .java 파일로 생성됨

- Class.java File은 해당 프로젝트 폴더/WEB-INF/classes/ 에 생성

- Web.XML은 해당 프로젝트 폴더/WEB-INF/ 에 생성

- Tomcat에 연동되지 않을 경우 작업창 하단 툴팁-Servers에 직접 올리기

- 대소문자 구분



주로 사용하는 코드들(HTML/JSP)

<META http-equiv="Content-Type"/ content = "text/html;charset=euc-kr"> => HTTP

String str = request.getParameter("[STR]"); => 입력된 값 불러옴

int num = Integer.paresInt(str); => String 타입의 데이터를 int 타입으로 변환


주로 사용하는 코드들(Servlet)

-------------------------------------------------------------------------------------

import javax.servlet.http.*;

import javax.servlet.*;

import java.io.*;


public class [ClassName] extends HttpServlet {


/**

* Constructor of the object.

*/

public [ClassName]() {

super();

}


/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}


/**

* The doGet method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


response.setContentType("text/html");

PrintWriter out = response.getWriter();

out

.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");

out.println("  <BODY>");

out.print("    This is ");

out.print(this.getClass());

out.println(", using the GET method");

out.println("  </BODY>");

out.println("</HTML>");

out.flush();

out.close();

}


/**

* The doPost method of the servlet. <br>

*

* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {


response.setContentType("text/html");

PrintWriter out = response.getWriter();

out

.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("<HTML>");

out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");

out.println("  <BODY>");

out.print("    This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println("  </BODY>");

out.println("</HTML>");

out.flush();

out.close();

}


/**

* Initialization of the servlet. <br>

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

-------------------------------------------------------------------------------------



한글 지원되게 하는 Servlet code

-------------------------------------------------------------------------------------

 request.setCharacterEncoding("euc-kr");

 response.setCharacterEncoding("euc-kr");

 response.setContentType("text/html;charset = euc-kr");

-------------------------------------------------------------------------------------



주로 사용하는 코드들(web.xml)

-------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" 

xmlns="http://java.sun.com/xml/ns/javaee" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

</web-app>

-------------------------------------------------------------------------------------



<FORM> 엘리먼트

-------------------------------------------------------------------------------------

<FORM>

<INPUT TYPE = TEXT>

<INPUT TYPE = PASSWORD>

<INPUT TYPE = RADIO>

<INPUT TYPE = CHECKBOX>

<SELECT>

<OPTION>A</OPTION>

<OPTION>B</OPTION>

<OPTION>C</OPTION>

</SELECT>

<TETXAREA ROWS = 5 COLS = 50></TEXTAREA>

<INPUT TYPE = RESET VALUE ="취소">

<INPUT TYPE = SUBMIT VALUE = "확인">

</FORM> 


<FORM ACTION =[경로명] METHOD = POST> => [경로명] 에 폼에서 일어난 이벤트 전송

POST 를 받을 경우 doPost를 호출해야 함

-------------------------------------------------------------------------------------