面试题练习(9)——简单介绍一下Servlet、Servlet的基本架构

简单介绍一下Servlet:

Java中servlet的简单介绍

servlet简单介绍

Servlet简单介绍(一)

Servlet的基本架构:

HttpServlet类是一个抽象类,可以从该类派生出一个子类来实现一个HttpServlet,接受来自Web站点的请求(该请求来自访问该Web站点的客户浏览器),并将处理后的响应结果发回Web站点(Web站点再将响应结果发送给客户浏览器),在HttpServlet的子类中,必须至少重载下表中的其中一种方法。
方法名            用途
doGet            如果Servlet支持Http GET请求,用于Http GET请求
doPost           如果Servlet支持Http POST请求,用于Http POST请求
doPut             如果Servlet支持Http PUT请求,用于Http PUT请求
doDelete        如果Servlet支持Http DELETE请求,用于Http DELETE请求
init和destroy   如果需要管理Servlet生命周期内所持有资源,可以重载这两个方法 
参考资料:通常,不重载service方法,对于上表中的每一种HTTP请求,service方法通过分派它们到相应的Handler线程(doXXX方法)来处理这些标准的HTTP请求。
同样地,通常也不重载doOptions和doTrace方法,service方法通过分派它们到doTrace和doOptions来支持HTTP1.1 TRACE和OPTIONS。
Servlet通常运行在多线程的服务器中,因此,所编写的Servlet代码必须能够处理并行请求和对数据资源的同步访问。共享资源包括内存中的数据(例如:实例或类变量)和外部对象(例如:文件、数据库连接或网络连接)。

Protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{}
从service方法授并处理HTTP GET请求。GET方法允许客户读取来自Web服务器的信息,客户通过传递一个带URL的查询字符串可以告诉服务器它需什么信息。
重载支持GET请求的doGet方法还将自动支持HTTP HEAD请求,HEAD请求也是一个GET请求,它得到的返回响应中只有一个请求头(header)字段,而没有响应信息的内容。
如果重载方法,应该从该请求读数据,在响应中设置整个headers,访问PrintWriter或输出流对象,最后写响应数据。当设置headers时,应确保包含content type和encoding。如果使用PrintWriter对象返回响应,在存取PrintWriter对象之前必须设置content type。
Servlet引擎必须在写响应数据之前写headers,因为在写数据之后headers随时都可能被刷新。
如果设置Content-Length头,Servlet可以使用一个持久稳固的连接来将其响应返回给客户机,动态地改善性能。
如果请求的格式不正确,doGet方法将返回一个HTTP BAD_REQUEST信息。
参数req为一个HttpServletRequest对象,含有客户的Servlet请求;resp是一个HttpServletResponse对象,含有Servlet发送给客户的响应。
简洁版:

package test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletName extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        }
}

或者

package com.ht.servlet;
import java.io.IOException;

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

public class ServletName extends HttpServlet{

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        super.doPost(req, resp);
    }
    }
这是新建Servlet默认的代码:

package com.ht.servlet;

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 test extends HttpServlet {

	/**
	 * 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();
	}
}

Servlet的基本架构或简单架构【主要是第一问的答案】

基本架构简单讲述,如下图:

可以了解下:面试题练习(13)——(含JAVA或JSP解决方案)主要七种乱码处理、get提交与post提交的区别、doPost,doGet的区别 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yvette_QIU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值