简体   繁体   English

在JSP中显示来自Java类的数据

[英]Display data from java class in JSP

I am trying to make a web project. 我正在尝试制作一个Web项目。 My question can I display java properties on the JSP page using JSTL? 我的问题是可以使用JSTL在JSP页面上显示Java属性吗? I am quite new in JSP. 我在JSP中还很陌生。
As far I am now , I created the login page which is handled by servlet. 到目前为止,我已经创建了由servlet处理的登录页面。 What I want to achieve is that user log in with his credentials. 我要实现的是该用户使用其凭据登录。 And based on the credentials the data are processed on the background and displayed on page. 基于凭据,数据将在后台处理并显示在页面上。 (after login user clicks on page my info and the data are automatically populated). (登录用户单击页面上的我的信息后,数据将自动填充)。 Creating another servlet is not a good option, so I probably need to call the data from the normal class(without needing a form). 创建另一个servlet并不是一个好的选择,因此我可能需要从普通类中调用数据(不需要表单)。

Login.jsp 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 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 (auth. handler) [i used static publicID because based on this, other stuff is done] 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;
        }
    }

}

This redirects me to the main.jsp. 这将我重定向到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 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;
    }

}

And finally. 最后。 UserInfo.jsp 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>

But this is always empty. 但这总是空的。 Can you please tell me where I am wrong? 你能告诉我我哪里错了吗?

UserInfoService解析为null ,您需要将其注册为bean, 请参见此处

您没有在代码中的任何地方调用UserInfoService.setUserName()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM