最近在做一个通过Servlet实现后台批量接收上传文件的东西,现将Servlet的开发、运行配置、调用记录下来。我们以最简单的FileUpload为例,目前所有的http协议相关的Servlet均继承于HttpServlet。

  • Servlet开发

Servlet的开发都必须将Servlet-api.jar引用到项目的lib目录中,具体如何引用,不详细描述。开发HttpServer必须扩展HttpServer类。HttpServlet 类包含

init():在 Servlet 的生命期中,仅执行一次 init() 方法。它是在服务器装入 Servlet 时执行的。 可以配置服务器,以在启动服务器或客户机首次访问 Servlet 时装入 Servlet。 无论有多少客户机访问 Servlet,都不会重复执行 init() 。缺省的 init() 方法通常是符合要求的,但也可以用定制 init() 方法来覆盖它。

destroy():destroy() 方法仅执行一次,即在服务器停止且卸装Servlet 时执行该方法。

service() :service() 方法是 Servlet 的核心。每当一个客户请求一个HttpServlet 对象,该对象的service() 方法就要被调用,而且传递给这个方法一个"请求"(ServletRequest)对象和一个"响应"(ServletResponse)对象作为参数。 在 HttpServlet 中已存在 service() 方法。缺省的服务功能是调用与 HTTP 请求的方法相应的 do 功能。
Servlet的响应可以是下列几种类型:一个输出流,浏览器根据它的内容类型(如text/HTML)进行解释。一个HTTP错误响应, 重定向到另一个URL、servlet、JSP。

doGet():当一个客户通过HTML 表单发出一个HTTP GET请求或直接请求一个URL时,doGet()方法被调用。与GET请求相关的参数添加到URL的后面,并与这个请求一起发送。当不会修改服务器端的数据时,应该使用doGet()方法。

doPost():当一个客户通过HTML 表单发出一个HTTP POST请求时,doPost()方法被调用。与POST请求相关的参数作为一个单独的HTTP 请求从浏览器发送到服务器。当需要修改服务器端的数据时,应该使用doPost()方法。

getServletConfig():GetServletConfig()方法返回一个 ServletConfig 对象,该对象用来返回初始化参数和ServletContext。ServletContext 接口提供有关servlet 的环境信息。

getServletInfo():它提供有关servlet 的信息,如作者、版本、版权。

serlet代码简单示例

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*; public class UploadFiles extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter(); String title = "Using GET Method to Read Form Data";
String docType = "<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n"; out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>First Name</b>: "+ request.getParameter("first_name") + "\n" +
" <li><b>Last Name</b>: "+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"</body></html>");
} @Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
} @Override
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
processRequest(request, response);
} @Override
public String getServletInfo() {
return "Hellow welcome!";
}// }
  • Servlet运行配置

在项目中的创建一个web.xml配置文件,存放在webcontent/WEB-INF/web.xml下。web.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>FileUpLoad</servlet-name>
<servlet-class>cn/com/Convert/Document/FileUpLoad</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FileUpLoad</servlet-name>
<url-pattern>/FileUpLoad</url-pattern>
</servlet-mapping>
</web-app>

配置说明:

1、servlet-name  节点为Servlet名称,可以自行任意定义,但必须保持<servlet>和<servlet-mapping>两个节点中的子节点<servlet-name>的节点值保持一致。

2、<servlet-class>节点中是配置servlet类的路径,即包 + 类名,如cn.com.Convert.Document/FileUpLoad 。

3、<url-pattern>节点中是配置servlet的虚拟路径,一般保持与servlet名称一致,在实际访问的地址为http://localhost:8080/项目名称/servlet名称

4、要使servlet生效必须重启tomcat,所有web.xml的修改都必须重启tomcat

  • Servlet3新特性

在Servlet3的新特性中,我们配置Servlet更加简单方便,只需要在Servlet类代码前面加上注解就可以,无需在进行web.xml配置。注解示例

@WebServlet(name="FileUpLoad", urlPatterns="/FileUpLoad")     其中name为servlet名称, urlPatterns为servlet访问路径

servlet还可以进行其他参数配置,诸如异步返回、设置

  • Servlet调用
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form id="frmPost" name="frmPost" action="http://localhost:8080/DocumentConvert/FileUpLoad" method="post" enctype="multipart/form-data" >
<input type="file" id="file1" name="file">
<input type="file" id="file2" name="file">
<input type="file" id="file3" name="file">
<input type="file" id="file4" name="file">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
  • Servlet参考资料

1·、https://www.ibm.com/developerworks/cn/java/j-lo-servlet/

2、http://www.yiibai.com/servlets/servlets_form_data.html

3、http://www.oschina.net/question/12_52027

4、http://baike.baidu.com/view/25169.htm

最新文章

  1. bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
  2. MVC开发模式之Servlet+jsp+javaBean
  3. selenium3.0.1调用firefox
  4. stl的优先级队列
  5. solr 导入数据
  6. STM32先设置寄存器还是先使能时钟
  7. php获取当前url完整地址
  8. Call Azure Queue get &quot;The remote server returned an error: (400) Bad Request.&quot;
  9. hdu 3018
  10. BAE 环境下配置 struts2 + spring + hibernate(SSH)(三)spring&amp;hibernate
  11. codility上的问题(26) Hydrogenium 2013
  12. logstash 输出到elasticsearch 自动建立index
  13. 201521123044 《Java程序设计》第10周学习总结
  14. Error 1313: RETURN is only allowed in a FUNCTION SQL Statement
  15. 老男孩Python全栈学习 S9 日常作业 004
  16. 现网环境业务不影响,但是tomcat启动一直有error日志,ERROR org.apache.catalina.startup.ContextConfig- Unable to process Jar entry [module-info.class] from Jar [jar:file:/home/iufs/apache-tomcat/webapps/iufs/WEB-INF/lib/asm
  17. netstat -na 查看有大量TIME_WAIT解决办法(修改内核参数)
  18. Codeforces.226D.The table(构造)
  19. BZOJ2079:[POI2010]Guilds(乱搞)
  20. Android从无知到有知——NO.3

热门文章

  1. 从UI Automation看Windows平台自动化测试原理
  2. struts文件上传拦截器中参数的配置(maximumSize,allowedTypes ,allowedExtensions)问题
  3. 前端图片预览,上传前预览,兼容IE7、8、9、10、11,Firefox,Chrome(学习到的知识)
  4. iOS 宏定义_16进制色值转化为RGB返回UIColor类型对象
  5. Linux 继续进阶
  6. Keil 的辅助工具和部份高级技巧
  7. SQL in查询报告类型转换失败的3种解决办法
  8. 【HDOJ】4902 Nice boat
  9. Fixing common issues when hosting a .NET 4.0 WCF service in IIS 7
  10. Spark使用CombineTextInputFormat缓解小文件过多导致Task数目过多的问题