`
艾莎文
  • 浏览: 11716 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Servlet学习笔记一

    博客分类:
  • java
阅读更多

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. <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
	}

}

 

实例3-02MyEclipse开发Servlet

Servlet的生命周期
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. <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>");
		
		int repeat = Integer.parseInt(this.getInitParameter("repeat"));
		for(int i = 0; i < repeat; i++){
			printContent(out);
		}
		
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	
	public void printContent(PrintWriter out){
		out.println("<table border='1'>");
		out.println("<tr><td>");
		out.println("欢迎访问我们的网站");
		out.println("</tr></td>");
		out.println("</table><br>");
	}
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @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. <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>");
		printContent(request,out);
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}
	
	
	public void printContent(HttpServletRequest request , PrintWriter out){
		out.println("Server name:" + request.getServerName() + "<br>");
		out.println("Server ip:" + request.getRemoteAddr() + "<br>");
		out.println("Server port:" + request.getServerPort() + "<br>");
		out.println("Server protocol:" + request.getProtocol()+ "<br>");
		out.println("Server contextpath:" + request.getContextPath() + "<br>");
		out.println("Server scheme:" + request.getScheme() + "<br>");
		out.println("Server method:" + request.getMethod() + "<br>");
		out.println("Server uri:" + request.getRequestURI() + "<br>");
		out.println("Server url:" + request.getServletPath() + "<br>");
	}

	/**
	 * 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
	}

}
 

实例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. <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>");
		printContent(request , out);
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	
	public void printContent(HttpServletRequest request , PrintWriter out){
		Enumeration e = request.getHeaderNames();
		String name="";
		out.println("<table border='1'>");
		while(e.hasMoreElements()){
			name = (String)e.nextElement();
			out.println("<tr><td>" + name + "<td>");
			out.println("<td>" + request.getHeader(name) + "</td>");
			out.println("</tr>");
		}
		out.println("</table><br>");
	}
	/**
	 * 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
	}

}
 
分享到:
评论

相关推荐

    JSP Servlet 学习笔记 第2版 林信良 随书光盘 共三卷

    JSP Servlet 学习笔记 第2版 林信良 随书光盘 第3卷

    Servlet学习笔记(java的Servlet)

    Servlet学习笔记(java的Servlet)。 jsp的学习基础。此笔记可让你5天内学会使用Servlet,可快速入门,也可当手册使用。 更多:http://download.csdn.net/user/daillo/all

    林学良Jsp&Servlet学习笔记

    **林学良Jsp&Servlet学习笔记**: 这份学习资料可能是对JSP和Servlet基础知识的系统性讲解,涵盖了上述概念和原理,适合初学者入门。通过阅读这份笔记,你可以了解JSP和Servlet的基本语法、生命周期、以及如何将它们...

    JSP_Servlet学习笔记(第2版).pdf

    《JSP & Servlet学习笔记(第2版)》是作者多年来教学实践经验的总结,汇集了学生在学习JSP&Servlet;或认证考试时遇到的概念、操作、应用等各种问题及解决方案。 《JSP & Servlet学习笔记(第2版)》针对Servlet 3.0的...

    JSP&Servlet学习笔记.pdf

    本书是作者多年来教学实践经验的总结,汇集了教学过程中学生在学习JSP & Servlet时遇到的概念、操作、应用或认证考试等问题及解决方案。  本书针对Servlet 3.0的新功能全面改版,无论是章节架构与范例程序代码,都...

    Java相关课程系列笔记之九Servlet学习笔记

    Java相关课程系列笔记之九Servlet学习笔记 Java相关课程系列笔记之十JSP学习笔记 Java相关课程系列笔记之十一Ajax学习笔记 Java相关课程系列笔记之十二jQuery学习笔记 Java相关课程系列笔记之十三Struts2学习笔记 ...

    [JSP&Servlet;学习笔记(第2版)]

    **JSP(Java Server Pages)与Servlet...这个**[JSP&Servlet学习笔记(第2版)]**会涵盖这些主题,并可能包括示例代码、最佳实践和常见问题解答,帮助读者全面掌握JSP和Servlet的使用技巧,进一步提升Java Web开发能力。

    吉林大学珠海学院JSP&Servlet学习笔记(第二版)课后答案

    吉林大学珠海学院JSP&Servlet学习笔记(第二版)课后答案 本资源为吉林大学珠海学院JSP&Servlet学习笔记(第二版)的课后答案,涵盖了JSP和Servlet相关的知识点。本笔记共分为九章,每章节都包含了相关的问题答案,...

    jsp&servlet学习笔记(第2版)光盘

    jsp&servlet学习笔记(第2版)光盘,买书附赠光盘,完片版本。

    Servlet学习笔记

    ### Servlet学习笔记精炼知识点 #### 一、Servlet初始化与加载机制 1. **Servlet注册与实例化**:在Web应用中,一个Servlet的生命周期始于它的注册与实例化过程。通常,这一过程是在Web服务器启动时,根据`web.xml...

    jsp/servlet个人学习笔记

    学习 JSP 和 Servlet 要注重理论与实践相结合,不仅要理解它们的工作原理,还要通过编写实际项目来提升技能。同时,不断关注新技术,如 Spring Boot 和微服务架构,这将有助于你更好地适应 Java Web 开发的未来趋势...

    [JSP&Servlet学习笔记(第2版)] 林信良

    [JSP&Servlet学习笔记(第2版)] 林信良

    JSP &amp; Servlet学习笔记(第2版)

    书 名:JSP & Servlet学习笔记(第2版) 作 者:(台湾)林信良 著 出 版 社:清华大学出版社 出版时间:2012-5-1 ISBN:9787302283669 纸书页数:456页 定 价:¥ 58.00 内容简介: 本书是作者多年来...

    JSP & Servlet学习笔记

    本书是作者多年来教学实践经验的总结,汇集了教学过程中学生在学习JSP & Servlet时遇到的概念、操作、应用或认证考试等问题及解决方案。, 本书针对Servlet 3.0的新功能全面改版,无论是章节架构与范例程序代码,都做...

    JSP_Servlet学习笔记(第2版)

    本书是作者多年来教学实践经验的总结,汇集了教学过程中学生在学习JSP &Servlet;时遇到的概念、操作、应用或认证考试等问题及解决方案。  本书针对Servlet 3.0的新功能全面改版,无论是章节架构与范例程序代码,都...

    JSP&Servlet学习笔记(第2版)_打印版

    根据提供的文件信息,标题为“JSP&Servlet学习笔记(第2版)_打印版”,描述指出文档经过空白边裁剪处理以便于打印。虽然提供的部分内文无法直接转化为明确的知识点,但我们可以根据标题和描述推测该文档的主要内容,...

    JSP Servlet学习笔记

    JSP Servlet学习笔记,java编程相关知识

Global site tag (gtag.js) - Google Analytics