Jsp中的Serverlet和Asp.net里的ashx文件有木有一点像啊? 有木有!
以前接触.net比较多,这几天学习Ajax,和身边的学Jsp的同学交流了一下。
了解一下Jsp的运行原理,发现其中的serverlet可以直接向浏览器输出,输出内容包括可以包括Html,其实就是可以动态构建页面。(当然Serverlet的功能不止这些)。这让我想到了asp.net中的*.ashx文件,他也是可以直接输出到浏览器动态构建页面。这就有点像了。
asp.net中的ProcessRequest 函数的参数HttpContext 是“封装有关个别 HTTP 请求的所有 HTTP 特定的信息”。而serlverlet中的doget,dopost函数中的参数:HttpServletRequest , HttpServletResponse 则分别和asp.net 中HttpContext 类中的request和reponse对应。
还是有点像的哈,大家说下自己的看法!
ashx文件:
<%@ WebHandler Language="C#" Class="Handler" %>using System;using System.Web;public class Handler : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.Write("Hello World"); } public bool IsReusable { get { return false; } }}
package dbdao.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.RequestDispatcher;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class Sample extends HttpServlet { public Sample () { super(); } public void destroy() { super.destroy(); } 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(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getParameter("name"); 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(); } public void init() throws ServletException { // Put your code here }}