源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

基于JSP HttpServlet的详细介绍

  • 时间:2022-01-08 09:39 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:基于JSP HttpServlet的详细介绍
[b]HttpServlet [/b]先来复习一下上一节提到的类结构图: [img]http://files.jb51.net/file_images/article/201304/2013042200000.png[/img] 可以看到,HttpServlet继承了GenericServlet,不过它也是一个抽象类, 不能直接使用,只能继承它。 [b]HttpServlet中常用的方法有两个:[/b] doGet void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 当浏览器用GET方式访问时,该方法被调用。 doPost void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 当浏览器用POST方式访问时,该方法被调用。 这两个函数内部的处理方法基本上与上一节介绍的GenericServlet.service()函数相同。 另外其他的HTTP请求也都有相应的方法:
HTTP请求类别 HttpServlet的方法
GET doGet()
POST doPost()
HEAD doHead()
PUT doPut()
DELETE doDelete()
[h2]HttpServletRequest[/h2] [code]doGet()[/code][code]doPost()[/code]函数的两个参数为[code]HttpServletRequest[/code][code]HttpServletResponse[/code]对象。 [code]HttpServletRequest[/code]接口表示浏览器请求,你可以通过这个类获取浏览器发送到服务器的任何信息。 对于PHP程序员来说,这个类有点类似于[code]$_GET[/code][code]$_POST[/code][code]$_SERVER[/code] 等几个变量的内容。 它的常用方法如下: [quote] [code]getParameter[/code] [code]String getParameter(String name)[/code] [/quote] 获取指定变量名name所对应的参数值。该方法实际上为父接口[code]javax.servlet.ServletRequest[/code]的方法。如果是GET请求则获取查询字符串后的参数,POST请求则获取<form>表单中的参数。类似于PHP的[code]$_GET[/code][code]$_POST[/code]数组。 [quote] [code]getParameterValues[/code] [code]String[] getParameterValues(String name)[/code] [/quote] 这个方法与[code]getParameter()[/code]类似。当你要获取<input type=”check”>这类会返回多个值的表单属性时,就应当用这个方法。 [quote] [code]getMethod[/code] [code]String getMethod()[/code] [/quote] 返回字符串[code]"GET"[/code]或[code]"POST"[/code]。 [quote] [code]getRequestURI[/code] [code]String getRequestURI()[/code] [/quote] 获取请求的URI(不包括查询字符串)。相当于PHP的[code]$_SERVER['REQUEST_URI'][/code]。 [quote] [code]getServletPath[/code] [code]String getServletPath()[/code] [/quote] 获取Servlet的路径。相当于PHP的[code]$_SERVER['PHP_SELF'][/code]。 [quote] [code]getPathInfo[/code] [code]String getPathInfo()[/code] [/quote] 获取PathInfo。相当于PHP的[code]$_SERVER['PATH_INFO'][/code]。 [quote] [code]setCharacterEncoding[/code] [code]void setCharacterEncoding(String new)[/code] [/quote] 设置请求的编码。需要处理汉字时务必要通过该方法设置正确的字符编码,否则将无法正确读取浏览器发过来的文字。 还有好多有用的方法大家可以自己去参考接口文档。

[h2]HttpServletResponse[/h2] HttpServletResponse接口则用于控制服务器发送给客户端的内容,相当于PHP的[code]echo[/code][code]header[/code]等函数。 [quote] [code]setContentType[/code] [code]void setContentType(String type)[/code] [/quote] 设置返回值的类型。通常的HTML内容可设置为[code]"text/html; charset=UTF-8"[/code]等,而动态生成的图片则可以设置为[code]"image/gif"[/code]等。输出汉字之前,务必要通过该方法指定输出的字符编码。相当于在PHP中写 [code]header("Content-Type: image/gif")[/code]。 [quote] [code]ServletOutputStream[/code] [code]ServletOutputStream getOutputStream() throws IOException[/code] [/quote] 向客户端发送二进制数据时,需要通过此方法获取输出流。 [quote] [code]getWriter[/code] [code]PrintWriter getWriter() throws IOException[/code] [/quote] 向客户端发送文本数据时,需要通过此方法获取输出流。

[h2]示例程序[/h2] 上一节建立Servlet时,我们先生成了Java代码,再将其添加到web.xml的Servlet节中。 实际上可以直接在web.xml的Servlet节中建立Servlet,Eclicpse会自动帮我们生成Java代码。 我们这次将建立一个表单提交程序,通过一个HTML表单提交数据,然后在Servlet中读出数据并显示出来。 首先右键单击WebContent目录,选择[b]New->HTML[/b],新建一个HTML文档,命名为 htmlpost.html。 下一步是选择HTML模板,直接用默认值即可。 然后编辑 htmlpost.html,可参考本节的源代码。源代码下载: [url=http://xiazai.jb51.net/201304/yuanma/httppost_jb51net.zip]httppost_jb51net.zip[/url] 右键单击Deployment Descriptor中的Servlets,选择[b]New->Servlet[/b]。 [img]http://files.jb51.net/file_images/article/201304/2013042200001.png[/img] 按照下图的样子,在Java package处输入包名 com.idv2.learnjsp,在 Class name处输入类名 HttpPost,单击Next。 [img]http://files.jb51.net/file_images/article/201304/2013042200002.png[/img] 出现Servlet映射的配置界面,输入适当的Description。注意下方的 URL Mappings,这个就是从浏览器访问该Servlet时使用的URL。 [img]http://files.jb51.net/file_images/article/201304/2013042200003.png[/img] 下一步选择新建的类的属性,通常选择默认即可。 不过我们的Servlet只需要处理POST方法,所以下方的重载列表中只需选择doPost即可。 [img]http://files.jb51.net/file_images/article/201304/2013042200004.png[/img] 最后单击Finish,即可完成Servlet的建立,并且Eclipse会自动在Java代码的src目录中生成HttpPost.java文件的框架。 编辑java代码,可以参考下面的源代码下载。 [url=http://xiazai.jb51.net/201304/yuanma/httppost_jb51net.zip]httppost_jb51net.zip[/url] 其实这段代码的主要内容就是通过 getParameter 或者 getParameterValues 方法来获取客户端提交的数据。其代码片段如下;
[u]复制代码[/u] 代码如下:
// 字符集   request.setCharacterEncoding("UTF-8");   // 从表单中获得数据   out.println("  <li>用户名: " + request.getParameter("username"));   out.println("  <li>密码: " + request.getParameter("password"));   out.println("  <li>确认密码: " + request.getParameter("confpass"));   // 获取复选框的表单选项   String interests[] = request.getParameterValues("interests");   out.println("  <li>兴趣爱好: <br/>");   if (interests != null) {       for (int i = 0; i < interests.length; i++) {           out.println(interests[i] + "<br/>");       }   }
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部