zoukankan      html  css  js  c++  java
  • Jsp servlet 值传递。。

    先新建一个动态WEB项目。

    展开WebRoot/index.jsp

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     3 <html>
     4 <body>
     5 <form action="${pageContext.request.contextPath}/Controller/servlet_Con" method="post">
     6 用户名:<input type="text" name="username">
     7 密 码:<input type="password" name="password">
     8 <input type="Submit" value="提交" name="Submit" >
     9 </form>
    10 </body>
    11 </html>

    新建一个servlet.

     1 package Controller;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletException;
     6 import javax.servlet.http.HttpServlet;
     7 import javax.servlet.http.HttpServletRequest;
     8 import javax.servlet.http.HttpServletResponse;
     9 //火影
    10 public class servlet_Con extends HttpServlet {
    11     private static final long serialVersionUID = 1L;
    12     private String message;
    13     public void doGet(HttpServletRequest request, HttpServletResponse response)
    14             throws ServletException, IOException {
    15         
    16         String name= request.getParameter("username");
    17         String password=request.getParameter("password");
    18         if("admin".equals(name)&&"123".equals(password))
    19         {
    20             message="密码正确,跳转成功";
    21             System.out.println(message);
    22             request.setAttribute( "message",message);
    23             request.getRequestDispatcher("/success/success.jsp").forward(request, response);
    24         }
    25         else
    26         {
    27             message="用户名或密码错误,跳转错误";
    28             System.out.println(message);
    29             request.setAttribute( "message",message);
    30             request.getRequestDispatcher("/error/error.jsp").forward(request, response);
    31         }
    32     }
    33     public void doPost(HttpServletRequest request, HttpServletResponse response)
    34             throws ServletException, IOException {
    35         this.doGet(request, response);
    36 
    37     }
    38 
    39 }

    分别建立两个。success.jsp error.jsp文件。用于,跳转页面。

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%
     3 String path = request.getContextPath();
     4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     5 %>
     6 
     7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     8 <html>
     9   <head>
    10     <base href="<%=basePath%>">
    11     <title>My JSP 'error.jsp' starting page</title>
    12   </head>
    13   <body>
    14   ${message }//接收servlet过来的值。
    15     <br> 
    16   </body>
    17 </html>

    测试:

    1.先访问看看。

    2.前台页面。自动跳转。

    3.后台控制台。

    ----------------------------------------------------------------------------------------------------

    The end...........Jsp 其实也要编译成servlet文件。

  • 相关阅读:
    ASM instance正常启动,但是用sqlplus 连不上的问题
    Ubuntu环境下,项目出现:Call to undefined function curl_init() 提示
    linux安装curl扩展
    https请求排错过程
    php-fpm.conf文件的位置在哪里
    如何查找php-fpm监听的端口
    laravel AppKernel.php中的middleware、middlewareGroups、routeMiddleware
    laravel项目数据库交互逻辑
    Laravel中APP_KEY起什么作用
    php 出现Warning: A non-numeric value encountered问题的原因及解决方法
  • 原文地址:https://www.cnblogs.com/1-Admin/p/6059783.html
Copyright © 2011-2022 走看看