簡體   English   中英

在JSP中顯示來自Java類的數據

[英]Display data from java class in JSP

我正在嘗試制作一個Web項目。 我的問題是可以使用JSTL在JSP頁面上顯示Java屬性嗎? 我在JSP中還很陌生。
到目前為止,我已經創建了由servlet處理的登錄頁面。 我要實現的是該用戶使用其憑據登錄。 基於憑據,數據將在后台處理並顯示在頁面上。 (登錄用戶單擊頁面上的我的信息后,數據將自動填充)。 創建另一個servlet並不是一個好的選擇,因此我可能需要從普通類中調用數據(不需要表單)。

Login.jsp

<form action="LoginServlet" method="post">
<table>
<tr><td>Login    : </td><td><input type="text" name="login"/></td></tr>
<tr><td>Password :</td><td><input type="password" name="password"/></td></tr>
<tr><td><input type="submit" value="Login"/></td><td></td></tr>
</table>
</form>

LoginServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
                                             throws ServletException, IOException {
        String login = request.getParameter("login");
        String password = request.getParameter("password");
        request.setAttribute("login", login);
        request.setAttribute("password", password);
        LoginService.setPublicID(login);
        boolean result = LoginService.Auth(login, password);
        System.out.println(result);
        if(result==true){
            getServletContext().getRequestDispatcher("/main.jsp").forward(request, response);
        }
        else{
            response.sendRedirect("login.jsp");
        }

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
                                           throws ServletException, IOException {
            doGet(request, response);

 }

LoginService.java(身份處理程序)[我使用了靜態publicID,因為基於此,其他工作已經完成]

public class LoginService {

    public static String publicID;

    public static String getPublicID() {
        return publicID;
    }
    public static void setPublicID(String login) {
        LoginService.publicID = login;
    }

    public static boolean Auth(String login, String password){

        System.out.println(password);
        if(password.equals("gw") && login.equals("servo")){
            return true;
        }
        else
        {
            return false;
        }
    }

}

這將我重定向到main.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" %>
<!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>Insert title here</title>
</head>
<body>
<h2>Login successful!</h2>
<a href="userinfo.jsp">UserInfo</a>
 Welcome <c:out value="${login}"/>

</body>
</html>

UserInfoService.java

public class UserInfoService {
    static String userName;
    static String lastName;
    static String mobile;
    static String email;


    public static String getUserName() {
        return userName;
    }
    public static void setUserName(String userName) {
        userName = LoginService.getPublicID();
        UserInfoService.userName = userName;
    }
    public static String getEmail() {
        return email;
    }
    public static void setEmail(String email) {
        UserInfoService.email = email;
    }

}

最后。 UserInfo.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" %>
    <%@ page import="org.UserInfo.UserInfoService"%>

<!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>Insert title here</title>
</head>
<body>
<c:out value="${UserInfoService.getUserName}"/>
</body>
</html>

但這總是空的。 你能告訴我我哪里錯了嗎?

UserInfoService解析為null ,您需要將其注冊為bean, 請參見此處

您沒有在代碼中的任何地方調用UserInfoService.setUserName()方法。

暫無
暫無

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

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