winfwu

导航

Servlet 小试牛刀(doGet,doPost)

实验说明:

      通过javax.servlet.http下的HttpServlet,HttpServletRequest,HttpServletResponse来完成一些常用Servlet实例

 

java代码部分

package com.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 HelloServlet extends HttpServlet {

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

        this.log("执行了doGet方法");
        execute(request,response);
    }

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

        this.log("执行了doPost方法");
        execute(request,response);
    }

    public long getLastModified(HttpServletRequest request){
        this.log("执行 getLastModified");
        return -1;
    }

    @SuppressWarnings("unused")
    private void execute(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
            response.setCharacterEncoding("utf-8");
            request.setCharacterEncoding("utf-8");
            
            StringBuffer requestURL=request.getRequestURL();//获取页面URL
            String method=request.getMethod();//获取访问方法
            String param=request.getParameter("param");//获取参数
            
            response.setContentType("text/html");//设置文档类型是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><body>");
            out.println("以"+method+"方式访问页面。获取的param参数为"+param+"</br>");
            out.println("<form action='"+requestURL+"' method='get'>" +
                    "<input type='text' name='param' value='param String'/>" +
                    "<input type='submit' value='get'/></form>" +
                    "");
            
            out.println("<form action='"+requestURL+"' method='post'>" +
                    "<input type='text' name='param' value='param String'/>" +
                    "<input type='submit' value='post'/></form>" +
                    "");
            
            out.println("<script>document.write('本页面最后更新时间:'+document.lastModified);</script>");
            out.println("</body>");
            out.println("</html>");        
    }
}

 

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/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
  <servlet>
    <servlet-name>HelloServlet</servlet-name>
    <servlet-class>com.servlet.HelloServlet</servlet-class>
    <init-param>
        <param-name>message</param-name>
        <param-value>welcome to FirstServlet!</param-value>
    </init-param>
      
       <init-param>
        <param-name>encoding</param-name>
        <param-value>utf-8</param-value>
    </init-param>
    <!-- 配置该Servlet的加载方式.1是tomcat启动时加载.0是第一次该方法是加载 -->
    <load-on-startup>1</load-on-startup>
    
  </servlet>
<!-- 可以通过 /servlet/HelloServlet 或是。jsp/.php等来访问-->
  <servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/servlet/HelloServlet</url-pattern>
    <url-pattern>/servlet/HelloServlet.jsp</url-pattern>
    <url-pattern>/servlet/HelloServlet.php</url-pattern>
    <url-pattern>/servlet/HelloServlet.aspx</url-pattern>
    <url-pattern>/servlet/HelloServlet.asp</url-pattern>
  </servlet-mapping>    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

重点code部分:

 (1)用getServletConfig().getInitParameter("encoding");//来获取web.xml中的初始化数据。

(2)StringBuffer requestURL=request.getRequestURL();//获取页面URL

(3)String method=request.getMethod();//获取访问方法

(4)String param=request.getParameter("param");//获取参数

 

posted on 2013-12-12 23:02  winfwu  阅读(491)  评论(0编辑  收藏  举报