0

I have one Java Web Application. My User_Objects class is given below

public class User_Objects {
    public String firstName;
    public String middleName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }
}

I forward request with my Java User_Objects to JSP with using following code

    User_Objects fillObj = fillData(request);
    request.setAttribute("reqObj", fillObj);
    RequestDispatcher view = request.getRequestDispatcher("/organization.jsp");
    view.forward(request, response);

public User_Objects fillData(HttpServletRequest request) {
    User_Objects fillObj = new User_Objects();

    try {
        fillObj.setFirstName(request.getParameter("txtFirstname"));
    } catch (Exception e) {
    }

    try {
        fillObj.setMiddleName(request.getParameter("txtMiddlename"));
    } catch (Exception e) {
    }

    return fillObj;
}

I successfully receive this Object into my JSP form. But I want to send this Object again to the Servlet and When I click on Submit button on my JSP form and try to get this Object into my Servlet using below code

User_Objects obj = (User_Objects) request.getAttribute("reqObj");

request.getAttribute("reqObj") gives me null

My JSP form code is given below

<%@ page import="otn.aitc.io.User_Objects" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!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>Organization</title>
<link rel="stylesheet" href="css/jquery-ui.css">
<script src="js/jquery-1.12.4.js"></script>
<script src="js/jquery-ui.js"></script>
<script>
    $(function() {
        var reqObj = '${reqObj}';
        var reqStatus = '${orgStatus}';
        var orgJson = '${reqOrgJson}';
        orgJson = orgJson.replace("/", "'");

        if(reqStatus != "" || orgJson != "") {
            if(reqStatus == "false") {
                document.getElementById("lblError").style.display = '';
            }
            else {
                document.getElementById("lblError").style.display = 'none';
            }

            var availableTutorials = [];
            if(orgJson != "") {
                var parsed = JSON.parse(orgJson);
                for(var x in orgJson) {
                    if(parsed[x] != undefined) {
                        availableTutorials.push(parsed[x]);
                    }
                }
            }

            $("#txtOrgName").autocomplete({source: availableTutorials});
        }
        else {
            window.location.href = "index.jsp";
        }
    });

    function validateOrg() {
        var orgName = document.getElementById("txtOrgName");
        if (orgName.value.trim().length == 0) {
            alert("Enter Org Name");
            return false;
        }
    }
</script>
</head>
<body>
    <form name="orgname" action="org_name" method="post">
        <table align="center">
            <tr align="center">
                <td colspan="4"><img src="images/logo.jpg" /></td>
            </tr>
            <tr>
                <td>Organization :</td>
                <td><input type="text" id="txtOrgName" name="txtOrgName" /><label style="color: red;">*</label></td>
            </tr>
            <tr>
                <td align="center" colspan=2><br/><input type="submit" name="btnOrgName" id="btnOrgName"
                    value="Validate" onclick="return validateOrg();" /></td>
            </tr>
        </table>
        <p align="center"><label id="lblError" style="color: red;">Error Message.</label></p>
    </form>
</body>
</html>

I am using Java with Eclipse.

user3441151
  • 1,880
  • 6
  • 35
  • 79

1 Answers1

1

Don't mix and match the different execution scopes.

The serlvet and the JSP page are executed on server side (actually the JSP page is compiled to a servlet, too). Once the HTTP request is finished all request based objects are discarded, including the request attributes. In the end it is a templating engine producing HTML text.

This HTML text (with embedded JavaScript) is executed by the client's browser. This execution scope is totally different to your server request scope, which has created this HTML page. So in your JavaScript code all the Java objects used to create that page on server side are unknown and unaccessable.

So you have two alternatives.

  1. While creating the HTML include enough information to recreate your server side object. E.g. if you have a Person object with name and age attributes, you can include name and age as hidden form fields. After submitting the form you can recreate the Person object with the hidden field values coming in as request parameters. Pro: Simple to implement. Con: Only feasable for small data. Data is exposed to the client and can be manipulated.

  2. Store the object on server side inside the session.
    Pro: Data is not exposed to the client. Con: Implementation more complex because of possible concurrent access to the session variables (browser can do multiple requests for the same session at the same time).

vanje
  • 10,180
  • 2
  • 31
  • 47