zoukankan      html  css  js  c++  java
  • 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园
  • 相关阅读:
    iOS微信支付集成
    iOS支付宝支付集成
    JavaScript原生实现《贪吃蛇》
    安装tensorflow的最简单方法(Ubuntu 16.04 && CentOS)
    Eclipse 插件管理
    settings.xml 文件配置
    Spring MVC 起步
    机器学习: KNN--python
    Python: PS 图像调整--亮度调整
    计算机设计思想 —— 代理(proxy)
  • 原文地址:https://www.cnblogs.com/yanglicyfsdm/p/4362550.html
Copyright © 2011-2022 走看看