Reputation: 1384
I have a Servlet thats run and takes in data from a web page. Later on in a different set of tasks I want to access this data from a standard Java class and use the data, how would i go about this? Can I save the data anywhere for access?
I have code like this :
String name = request.getParameter("username");
And then I tried to set it as an attribute and pass it :
getServletContext().setAttribute("com.mycompany.app-param", "name");
Then in the next class I tried to access the context to get the varible, but no matter what I tried I got server error 500's, or null or Server = null. i don't think it's able to pick up the context properly :
value = getServletContext().getAttribute("com.mycompany.app-param");
Does anyone know how I can access the context created previously and get its variable?
Upvotes: 1
Views: 1196
Reputation: 7899
you can write a data access layer with one dao which will persist the data in database . If your requirement is to use data only at session scope then you can save that data in session.
Upvotes: 1
Reputation: 32953
See Is getServletContext() officially supported in GAE? - on gae it's not guaranteed to work the way you want it to work (eg to pass on info).
You should use Session or Request scoped attributes instead, see here here and here to get you started. Which one you use depends on the required life time and visibility of the saved attributes (Request < Session < Application)
Upvotes: 1
Reputation: 1956
Here you go...
public class DummyDTO {
private String name = null;
private String age = null;
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
public class MyServletClass extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
DummyDTO dummyDTO = new DummyDTO();
dummyDTO.setName(request.getParameter("name"));
dummyDTO.setAge(request.getParameter("age"));
AnotherClass.setValues(dummyDTO);
}
public class AnotherClass {
String name = "";
String age = "";
public static void setValues(DummyDTO dummyDTO) {
name = dummyDTO.getName();
age = dummyDTO.getAge();
}
}
Upvotes: 1
Reputation: 5496
You can save that data in some file
Or
You can provide Class having all properties in UI and create a object for that class and serialize that object at that moment after that when ever you want that data you can desrialize that object.
Serialization link http://www.tutorialspoint.com/java/java_serialization.htm
Upvotes: 1
Reputation: 1956
You can make use of a DTO with a set of getters(getting data to java class) and setters(setting data from web page) and maintain it a global scope so that you can access it later using the Java class...Hope this helps!!!
Upvotes: 1
Reputation: 5145
Yes, like in a database table? Or even a file?
Sample code:
https://www.ibm.com/developerworks/mydeveloperworks/blogs/wasdev/entry/servlet_jdbc_sample1?lang=en
Upvotes: 1