3

I've got a forum where a user can register there details and this gets sent to a Servlet and then a Java bean. What I'm having trouble with is that I can't get the data to be displayed on another JSP page when the Java bean is requested. So CreateAccount.jsp allows the user to input into the forum.

The forum is posted to a Servlet(RegisterDetails.java) and sends it to a Java bean (Register.java). Then error.jsp shows this data from the bean. Below is my code. The current code shows each value as 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>
Tiny
  • 27,221
  • 105
  • 339
  • 599
user3258979
  • 87
  • 1
  • 1
  • 6
  • Since you're creating a new instance of `RegisterDetails` in your Servlet, the `` tag is worth nothing. You can safely remove it. You can access the properties of the bean like, `((RegisterDetails)request.getAttribute("details")).getfullName()` and the same for the rest of the fields. Conventionally, the names of input fields should begin with a small case letter. Also, if you use JSTL/EL then, you can simply get the properties of `RegisterDetails` like `${details.fullName}`, `${details.emailAddress}`, `${details.password}` and `${details.username}`. – Tiny Feb 04 '14 at 01:33

2 Answers2

4

First of all, your class RegisterDetails is not a bean because it does not implement the java.io.Serializable interface.

Secondly, you really need to implement some kind of input sanitization for the data coming from the request (to prevent SQL/HTML/Cross-site scripting injections), and if you are going to use beans I guess you should put that inside the bean class.

Third, you could also dispense with the bean concept (meaning you won't need jsp:useBean) and just save the instance of the class RegisterDetails as a regular class object into the session, and then you can pull it from the session on any page as follows:

In the servlet:

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

In any other page:

RegisterDetails details = (RegisterDetails)session.getAttribute("details");
developerwjk
  • 8,619
  • 2
  • 17
  • 33
  • I was going to suggest this too. Something like: HttpSession session = request.getSession(); RegisterDetails details = new RegisterDetails(); details.setName(request.getParameter("FullName")); details.setEmail(request.getParameter("EmailAddress")); details.setPassword(request.getParameter("Password")); details.setUsername(request.getParameter("Username")); session.setAttribute("details", details); – John Lee Feb 04 '14 at 00:22
  • He's already setting all the values in a variable details, but then is saving that in the request object rather than session object. So he can just change to saving it in the session object. – developerwjk Feb 04 '14 at 00:27
  • Since the request is just forwarded (not redirected using `response.sendRedirect()`), the object of `RegisterDetails` stored as a request attribute like `request.setAttribute("details", details);` can be available to JSP. There should not be necessary to store it into the session. Please don't mind. – Tiny Feb 04 '14 at 00:58
  • The jsp business is all new to be and I'm trying to understand how jsp/servlets/beans are meant to interact before I get onto security and using a database. Anyway I will update my code with using session attribute as this now works. However, I would still like to know how I could still do the way I was doing it without a session. Thank you. – user3258979 Feb 04 '14 at 16:03
  • If you don't want to use the session, keep saving the bean in the request object in your servlet and forwarding to the JSP, then in the JSP do `RegisterDetails details = (RegisterDetails)request.getAttribute("details");' – developerwjk Feb 06 '14 at 00:24
-2

developerwjk - good catch on the bean not being serializeable, but that shouldnt cause the problem.

user3258979 - your servlet looks ok to me. And I can see your form doing the proper POST. Have you tried doing a trace on the servlet? Can you put a break point on this line to confirm the request values are coming through:

details.setName(request.getParameter("FullName"));

John Lee
  • 1,357
  • 1
  • 13
  • 26
  • I have tested it so that you are able to print the values of the form from the servlet in the console. I have also tested it so if I were to use getUsername() from the bean it would also get the data. – user3258979 Feb 04 '14 at 15:08