Reputation: 1074
My jsp web application takes input from html input and keep it in the memory with the press of the button, so actually i push the values in the memory and keeps it untill reset is pressed. The problem comes when i goto another machine and run that application, i get the same values that were entered by the previous user.
eg:
main.jsp has some input fields and when i click 'add' those values from html input is stored in memory objects, and is showed in the html inputs till the memory is not cleared.
Now from another machine, i access this application and go to jsp file, there i get prefilled html input boxes having same values.
I know it is caused by i initializing the class object inside jspInit() function.
I have made sample project to show you the problem.
Test.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="test.TestClass" %>
<!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>
<%!
TestClass objTest;
public void jspInit()
{
objTest = new TestClass();
}
%>
<form action="Test.jsp">
value1: <input type="text" name="txtValue1" value=" <%out.print(objTest.value1);%>"/>
value2: <input type="text" name="txtValue2" value=" <%out.print(objTest.value2);%>"/>
<input type="submit" name="btnSubmit"/>
</form>
<%
if(request.getParameter("btnSubmit")!=null)
{
String str1 = request.getParameter("txtValue1");
String str2 = request.getParameter("txtValue2");
objTest.add(str1, str2);
}
%>
</body>
</html>
TestClass.java
package test;
public class TestClass {
public String value1 ="no value";
public String value2 ="no value";
public void add(String str1, String str2)
{
value1 = str1;
value2 = str2;
}
}
why am i not getting a new page everytime i go from a different machine.The jspInit() function makes the object static and all its values are retained.
I need this jspInit() function because i want to retain the values for that particular user so that when he refreshes the page the form values are not gone.
How can i make jspInit() to retain value only for that particular browser request. Also what other approaches i can use.
Also the actual application is very big, so if possible please suggest some workaround for this approach.
P.S: A newbie to jsp.
P.S: I know using scriptlets is not a good approach.
Thanks in advance.
Upvotes: 0
Views: 141
Reputation: 3830
The jspInit method is common for all the threads of your servlet class. Its use is basically for getting database connections and reader writer objects for a common resource. Your requirement is for every individual client of your server (i.e. different machine), for this you must use session object to store the parameters or use a Java Bean object.
You can refer this for good knowledge.
Upvotes: 1
Reputation: 17839
may be you want to use the session
Object to store values related to each session.
You can add session attributes like this: session.setAttribute("IDxxx",1)
and you can retrieve Objects from session like this int id= Integer.parseInt(session.getAttribute("IDxxx").toString())
For more on jsp implicit Objects see here and here and here
Upvotes: 0