본문 바로가기

Study/Programming

servlet 기본 골격


# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/bin:/usr/local/jdk1.6.0_13/bin

export CLASSPATH=.:/home/whitefox/apache-tomcat-5.5.27/common/lib/servlet-api.jar
export JAVA_HOME=/usr/local/jdk1.6.0_13
export CATALINA_HOME=/home/whitefox/apache-tomcat-5.5.27
export PATH



website1/WEB-INF/web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
     <servlet>
        <servlet-name>hello1</servlet-name>
        <servlet-class>servlets.HelloServlet1</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello1</servlet-name>
        <url-pattern>/hello1</url-patten>
    </servlet-mapping>
</web-app>





package servlets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;

public class HelloServlet1 extends HttpServlet{
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        //Get 방식의 요청일 때 호출
        doProcess(request, response);
   
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException   
    {
        //Post 방식의 요청일 때 호출
        doProcess(request, response);
    }

    public void doProcess(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        //모든 요청을 받아들임
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html:charset=utf-8");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("</head>");
        out.println("<body>");
        out.println("<b>난 서블릿으로 호출된 문서</b>");
        out.println("</body>");
        out.println("</html>");
        out.close();
    }
}

'Study > Programming' 카테고리의 다른 글

JSP 게시판 만들기  (2) 2009.07.27
JAVA Study  (0) 2009.07.21
JSP webprj1  (0) 2009.06.09
jsp 회원가입폼 만들기  (0) 2009.06.04
JSP 쿠키  (0) 2009.06.04