Servlet 학습 노트 1

12867 단어 자바
Servlet 인 스 턴 스 3 - 01 Servlet 작성
package com.v512.project1;

import java.io.IOException;
import java.io.PrintWriter;

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

public class FirstServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public FirstServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * 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(""-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * The doPost method of the servlet.
* * 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(""-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }

 인 스 턴 스 3 - 02MyEclipse 개발 ServletServlet 의 수명 주기 init () doGet () destroy () 인 스 턴 스 3 - 03 Servlet 에서 초기 화 된 인 자 를 가 져 옵 니 다.
package com.v512.project1;

import java.io.IOException;
import java.io.PrintWriter;

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

public class GetInitParameterServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4549363736397584651L;

	/**
	 * Constructor of the object.
	 */
	public GetInitParameterServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * 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(""-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * The doPost method of the servlet.
* * 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(""-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println(""); out.println(" A Servlet"); out.println(" "); int repeat = Integer.parseInt(this.getInitParameter("repeat")); for(int i = 0; i < repeat; i++){ printContent(out); } out.println(", using the POST method"); out.println(" "); out.println(""); out.flush(); out.close(); } public void printContent(PrintWriter out){ out.println(""); out.println(""); out.println("
"); out.println(" "); out.println("

"); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }

 인 스 턴 스 3 - 04 Servlet 서버 실행 매개 변수 가 져 오기
package com.v512.project1;

import java.io.IOException;
import java.io.PrintWriter;

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

public class GetServletInfoServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4201393896610492180L;

	/**
	 * Constructor of the object.
	 */
	public GetServletInfoServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * 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(""-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println(""); out.println(" A Servlet"); out.println(" "); printContent(request,out); out.println(" "); out.println(""); out.flush(); out.close(); } public void printContent(HttpServletRequest request , PrintWriter out){ out.println("Server name:" + request.getServerName() + "
"); out.println("Server ip:" + request.getRemoteAddr() + "
"); out.println("Server port:" + request.getServerPort() + "
"); out.println("Server protocol:" + request.getProtocol()+ "
"); out.println("Server contextpath:" + request.getContextPath() + "
"); out.println("Server scheme:" + request.getScheme() + "
"); out.println("Server method:" + request.getMethod() + "
"); out.println("Server uri:" + request.getRequestURI() + "
"); out.println("Server url:" + request.getServletPath() + "
"); } /** * The doPost method of the servlet.
* * 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(""-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
 
인 스 턴 스 3 - 05 Servlet 헤드 정보 획득
 
package com.v512.project1;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

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

public class GetHeaderInfoServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public GetHeaderInfoServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet.
* * 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(""-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println(""); out.println(" A Servlet"); out.println(" "); printContent(request , out); out.println(" "); out.println(""); out.flush(); out.close(); } public void printContent(HttpServletRequest request , PrintWriter out){ Enumeration e = request.getHeaderNames(); String name=""; out.println(""); while(e.hasMoreElements()){ name = (String)e.nextElement(); out.println(""); out.println(""); } out.println("
" + name + ""); out.println("" + request.getHeader(name) + "

"); } /** * The doPost method of the servlet.
* * 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(""-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println(""); out.println(" A Servlet"); out.println(" "); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" "); out.println(""); out.flush(); out.close(); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }

좋은 웹페이지 즐겨찾기