简体   繁体   中英

Display data from java class in JSP

I am trying to make a web project. My question can I display java properties on the JSP page using JSTL? I am quite new in JSP.
As far I am now , I created the login page which is handled by 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).

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

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.

<%@ 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;
    }

}

And finally. 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()方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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