gpt4 book ai didi

jsp - 从 Java bean 获取数据以显示在 JSP 页面上

转载 作者:行者123 更新时间:2023-12-03 04:02:12 25 4
gpt4 key购买 nike

我有一个论坛,用户可以在其中注册详细信息,并将其发送到 Servlet,然后发送到 Java bean。我遇到的问题是,当请求 Java bean 时,我无法将数据显示在另一个 JSP 页面上。所以CreateAccount.jsp允许用户在论坛中输入内容。

论坛发布到 Servlet( RegisterDetails.java ) 并将其发送到 Java bean ( Register.java )。然后error.jsp显示来自 bean 的数据。下面是我的代码。当前代码将每个值显示为 null .

Register.java :

package com.cassandra.MrBlabber.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

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

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RegisterDetails details = new RegisterDetails();
details.setName(request.getParameter("FullName"));
details.setEmail(request.getParameter("EmailAddress"));
details.setPassword(request.getParameter("Password"));
details.setUsername(request.getParameter("Username"));
request.setAttribute("details", details);
getServletContext().getRequestDispatcher("/WEB-INF/Error.jsp").forward(request, response);
}
}

RegisterDetails.java :

package com.cassandra.MrBlabber.servlets;

public class RegisterDetails {
private String fullName;
private String emailAddress;
private String password;
private String username;

public RegisterDetails() {}

public String getName() { return fullName; }
public String getEmailAddress() { return emailAddress; }
public String getPassword() { return password; }
public String getUsername() { return username; }

public void setName(String value) { this.fullName = value; }
public void setEmail(String value) { this.emailAddress = value; }
public void setPassword(String value) { this.password = value; }
public void setUsername(String value) { this.username = value; }
}

CreateAccount.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="/MrBlabber/css/stylesheet.css"/>
<title>MrBlabber/Create an Account</title>
</head>
<body>
<!-- Section -->
<section>
<!-- Sign Up -->
<article>
<div id="articleWrapper">
<h3>No Account? Sign Up</h3>
<form id="createAccount" onsubmit="return validateForm();" action="Register" name="createAccount" method="POST">
<input type="text" id="name" name="FullName" placeholder="Full Name"/>
<input type="text" id="email" name="EmailAddress" placeholder="Email Address"/>
<input type="password" id="password" name="Password" placeholder="Create a password"/>
<input type="text" id="username" name="Username" placeholder="Choose your username"/>
<input type="submit" value="Sign up for MrBlabber"/>
</form>
</div>
</article>

<article>
<div id="errorMessage" class="errorMessage">
<script src="/MrBlabber/javascript/CreateAccountValidation.js" type="text/javascript"></script>
</div>
</article>
</section>
</body>
</html>

Error.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="com.cassandra.MrBlabber.servlets.RegisterDetails" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>
<h1>shit</h1>

<jsp:useBean id="RegisterDetails" class="com.cassandra.MrBlabber.servlets.RegisterDetails" scope="session"/>
<jsp:setProperty name="RegisterDetails" property="*"/>
<h1>
Name: <%=RegisterDetails.getName()%><br>
Email: <%=RegisterDetails.getEmailAddress()%><br>
Password: <%=RegisterDetails.getPassword()%><br>
Username: <%=RegisterDetails.getUsername()%><br>
</h1>
</body>
</html>

最佳答案

首先,您的类 RegisterDetails 不是一个 bean,因为它没有实现 java.io.Serialized 接口(interface)。

其次,您确实需要对来自请求的数据实现某种输入清理(以防止 SQL/HTML/跨站点脚本注入(inject)),如果您打算使用 beans,我想您应该这样做在 bean 类中。

第三,您还可以放弃 bean 概念(这意味着您不需要 jsp:useBean),只需将 RegisterDetails 类的实例作为常规类对象保存到 session 中,然后您可以从任意页面上的 session 如下:

在 servlet 中:

 session.setAttribute("details", details); //saving your object to the session

在任何其他页面中:

RegisterDetails details = (RegisterDetails)session.getAttribute("details");

关于jsp - 从 Java bean 获取数据以显示在 JSP 页面上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21540130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com