Jsp servlet 值傳遞。。

來源:互聯網
上載者:User

標籤:images   form   forward   equal   4.0   val   attr   error   cti   

先建立一個動態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 else26  {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檔案。

Jsp servlet 值傳遞。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.