@Beeder
2017-11-29T14:24:09.000000Z
字数 4039
阅读 657
javaWeb
Servlet是运行在服务器端的一小的Java程序,接收和响应从客户端发送请求.
Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中间层。
使用 Servlet,您可以收集来自网页表单的用户输入,呈现来自数据库或者其他源的记录,还可以动态创建网页。
void init(ServletConfig config):初始化/** 初始化方法* 执行者:服务器* 执行次数:一次* 执行时机:默认第一次访问的时候*/void service(ServletRequest request,ServletResponse response):服务 处理业务逻辑/** 服务* 执行者:服务器* 执行次数:请求一次执行一次* 执行时机:请求来的时候*/void destroy():销毁/** 销毁* 执行者:服务器* 执行次数:只执行一次* 执行时机:当servlet被移除的时候或者服务器正常关闭的时候*/public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {// Servlet 代码/** GET 请求来自于一个 URL 的正常请求,或者来自于一个未指定 METHOD 的 HTML 表单,它由 doGet() 方法处理。*/}public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {// Servlet 代码/** POST 请求来自于一个特别指定了 METHOD 为 POST 的 HTML 表单,它由 doPost() 方法处理。*/}
然后 Servlet 容器处理由多个线程产生的多个请求,每个线程执行一个单一的 Servlet 实例的 service() 方法。
// 导入必需的 java 库import java.io.*;import javax.servlet.*;import javax.servlet.http.*;//继承HttpServlet类public class HelloServlet extends HttpServlet{//处理 GET 方法请求的方法@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("GET请求已收到............");// 接受GET参数String value = req.getParameter("username");// 设置响应内容类型// response.setContentType("text/html");response.setContentType("text/html;charset=utf-8");// 实际的逻辑是PrintWriter out = response.getWriter();out.println("<h1>" +"值:"+ value + "</h1>");}//处理 POST 方法请求的方法@Overridepublic void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {System.out.println("POST请求已收到............");}}
配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><servlet><!-- 类名 --><servlet-name>HelloServlet</servlet-name><!-- 所在的包 --><servlet-class>com.runoob.test.HelloServlet</servlet-class></servlet><servlet-mapping><servlet-name>HelloServlet</servlet-name><!-- 访问的网址 --><url-pattern>/TomcatTest/HelloServlet</url-pattern></servlet-mapping></web-app>
使用 HTML 表单和提交按钮传递两个值。我们将使用相同的 Servlet HelloForm 来处理输入。
<!DOCTYPE html><html><head><meta charset="utf-8"><title>GET方法实例</title></head><body><!-- 使用POST方法:method="POST" --><!--<form action="HelloForm" method="POST">--><form action="HelloForm" method="GET">网址名:<input type="text" name="name"><br />网址:<input type="text" name="url" /><input type="submit" value="提交" /></form></body></html>
package com.runoob.test;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/*** Servlet implementation class HelloForm*/@WebServlet("/HelloForm")public class HelloForm extends HttpServlet {private static final long serialVersionUID = 1L;protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 设置响应内容类型response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();String title = "使用 POST 方法读取表单数据";// 处理中文String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");String docType = "<!DOCTYPE html> \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>站点名</b>:"+ name + "\n" +" <li><b>网址</b>:"+ request.getParameter("url") + "\n" +"</ul>\n" +"</body></html>");}}
设置TomcatTest项目下对应的 web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app><servlet><!-- 类名 --><servlet-name>HelloForm</servlet-name><servlet-class>com.runoob.test.HelloForm</servlet-class></servlet><servlet-mapping><!-- 访问的网址 --><servlet-name>HelloForm</servlet-name><url-pattern>/TomcatTest/HelloForm</url-pattern></servlet-mapping></web-app>