• 3.servlet实现页面的跳转


    效果:
    在网页的输入框中输入lily,跳到成功的页面(请求转发),输入的不是lily则跳到百度的页面(跳到工程之外的页面,则是请求重定向)

      1.建Web project“2Servlet_Basic”,在src文件夹下建MyHtml.html
     
    <!DOCTYPE html>
    <html>
      <head>
        <title>MyHtml.html</title>
     
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
       
        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
      </head>
     
      <body>
        <form action="/2Servlet_Basic/servlet/basic" method="post">
         <input type="submit" value ="click">
         usename:
            <input type="text" name="usename" >
        </form>
      </body>
    </html>
    <!--success.html-->
    <!DOCTYPE html>
    <html>
      <head>
        <title>success.html</title>
     
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
       
        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
      </head>
     
      <body>
        success! <br>
      </body>
    </html>
    <!--failure.html-->
    <!DOCTYPE html>
    <html>
      <head>
        <title>failure.html</title>
     
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
       
        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
      </head>
     
      <body>
        failure! <br>
      </body>
    </html>

    2.在web.xml中加入usename的局部变量(是这个servlet实例能够调用它)

    <?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"> 
    <servlet> 
    <servlet-name>basicservlet</servlet-name> 
    <servlet-class>com.amaker.servlet.ServletBasic</servlet-class> 
    <init-param> 
    <param-name>usename</param-name> 
    <param-value>lily</param-value> 
    </init-param> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>basicservlet</servlet-name> 
    <url-pattern>/servlet/basic</url-pattern> 
    </servlet-mapping> 
    <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    <login-config> 
    <auth-method>BASIC</auth-method> 
    </login-config> 
    </web-app>

    3..在工程下建包com.amaker.servlet,在包下建类“ServletBasic.java”

    //ServletBasic.java
    package com.amaker.servlet;
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    //继承HttpServet,doGet alt+/,sysout alt+/
    public class ServletBasic extends HttpServlet {
     /**
      *
      */
     private static final long serialVersionUID = 3616093822222878695L;
     public void init() throws ServletException {
       System.out.println("init");
     }
     protected void doGet(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
       System.out.println("doGet");
     }
     protected void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
       System.out.println("doPost");
     }
     protected void service(HttpServletRequest  request, HttpServletResponse response)
       throws ServletException, IOException {
       String usename=request.getParameter("usename");
       if(usename!=null&usename.equals("lily")){
        request.getRequestDispatcher("/success.html").forward(request, response);
    //跳到工程里的页面,叫做请求转发
       }else{
    //     request.getRequestDispatcher("/failure.html").forward(request, response); 
        response.sendRedirect("http://www.google.cn");
    //跳到工程之外的页面,请求重定向
       }
    //如果表单上填写的usename 为Lily,则跳转到成功的页面,否则跳转到失败的页面
    //提交表单后第一个将调用service方法
     }
     public void destroy() {
       System.out.println("destroy");
     }
     
     
    }
    ------------------------------------------------------------------------------------------------------------------------------本娃的学习日记@lily园
  • 相关阅读:
    日期时间工具类
    jQuery 使用attr()方式设置 checked 失效原因及解决方法
    vue总结
    【笔记】golang中使用protocol buffers的底层库直接解码二进制数据
    【笔记】对golang的大量小对象的管理真的是无语了……
    【记录一个问题】thanos receiver在更换tsdb文件后,内存并未显著下降
    【笔记】论文阅读:《Gorilla: 一个快速, 可扩展的, 内存式时序数据库》
    【分享】thanos receiver的grafana报表配置
    【采坑小计】thanos receiver的官方文档中,并未说明tsdb落盘的配置方式
    【记录一个问题】thanos receiver在tsdb切换期间,导致remote write接口失败增加
  • 原文地址:https://www.cnblogs.com/yanglicyfsdm/p/4362550.html
Copyright © 2020-2023  润新知