• 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");//获取参数

  • 相关阅读:
    PbootCMS按钮权限控制
    vueadmintemplate顶部一级菜单 侧栏二级菜单
    git常用命令
    MeEdu开发插件之【视图】
    MeEdu开发插件
    MeEdu开发插件之【模型】
    .Net 5.0 通过IdentityServer4实现单点登录之客户端通过授权码换取IdToken和AcessToken并访问Api部分源码解析
    供应链需求调研CheckList
    云计算参考架构几例
    数据库 随机字符串
  • 原文地址:https://www.cnblogs.com/DeepBlues/p/3472207.html
Copyright © 2020-2023  润新知