簡體   English   中英

將值從jsp發送到servlet,然后再返回到jsp

[英]Sending values from jsp to servlet then back to jsp

我知道之前已經問過類似的問題,但是由於某種原因,它不起作用。 我只是想檢查用戶是否在登錄頁面上輸入了兩個字段。 如果不是,那么我想在jsp上顯示消息,說他們需要輸入兩個憑據。 這是我的JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login</title>
</head>
<body>
    <h2>Some App</h2>
    <form action="login" method="post">
        <table>
            <tr>
                <td>Username</td>
                <td><input type="text" name="uname"></td>
            </tr>
            <tr>
                <td>Password</td>
                <td><input type="password" id="pass" name="pass"></td>
            </tr>
        </table>
        <br> <input type="button" value="Submit">
    </form>
    <c:out value= "${error}"/>
</body>
</html>

然后是servlet:

@WebServlet("/login")
public class Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession();

        //Getting values from the form
        String username = request.getParameter("uname");
        String password = request.getParameter("pass");

        System.out.println("Username is: "+ username);
        System.out.println("Password is: "+ password);

        //User user = new User();

        if((username.equals(""))|| password.equals("")){

            String message = "Please enter both the credentials";
            request.setAttribute("error", message);
            //RequestDispatcher rd = request.getRequestDispatcher("/login.jsp");
            //rd.forward(request, response);
            getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);

        }
        else{
            RequestDispatcher rd = request.getRequestDispatcher("/index.jsp");
            rd.forward(request, response);
        }

        //Setting values in the session
        session.setAttribute("username", username);
        session.setAttribute("password", password);
    }
}

由於我正在嘗試使用Maven,因此這是為jstl jar添加依賴項的pom.xml文件:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>

為什么該c:out標簽不打印任何內容? 我究竟做錯了什么? 任何幫助將不勝感激。 謝謝。

您正在混合兩種向客戶端返回響應的樣式。 嘗試使用以下代碼傳遞錯誤消息:

if((username == null)|| password == null) {              
    String message = "Please enter both the credentials";
    request.setAttribute("error", message);
    getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
}

經驗法則是,你必須使用request.setAttribute()forward()request.getSession().setAttribute()response.sendRedirect()

請嘗試使用$ {sessionScope ['error']}而不是$ {error}。 如果可行,那么您可能在另一個作用域中擁有另一個具有相同名稱“錯誤”的屬性

您還應該檢查usernamepassword是否為空。 (username != null && username.trim().isEmpty())

每個人的建議都很有價值。 我做錯了一些事情:不正確的servlet映射(@WebServlet修復了它),將代碼更改為@NaMaN和@bphilipnyc的答復,由於無法到達servlet而導致的最大錯誤是我遇到了輸入類型作為按鈕在jsp中。 我將其更改為輸入類型Submit,然后工作了。 謝謝你們的回復。 真的很感激。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM