[英]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.