So I'm using a bean on a JSP page to store some data, like so:
<jsp:useBean id="data" class="myclass" scope="session" />
Is there anyway to access this bean from a servlet at a later time in the same session?
EDIT:
Apparently I'm not accessing the same session when I load the jsp page and the servlet. I'm printing out the session ID and it's giving me a different value for both pages, so I can't access the bean. Any ideas?
Yes, you can obtain it as attribute from the session by the id as key.
Data data = (Data) request.getSession().getAttribute("data");
Note that you need to put classes in a package, else you cannot import it. You'd also like to give it a more sensible name than myclass.
Related
I am currently creating a web application use java servlets and jspx pages. I have created a java class with a parameter when called with the class being LessonSelection(int owner). I know that you cannot use a bean with a parameter so I made an object of that class with the parameter and then added it to the session, then used that session attribute in the useBean.
I have an error coming up saying 'The value of the useBean class attribute ${selected} is invalid'
The useBean code is below.
<jsp:useBean class="${selected}" id="timetable" scope="session"/>
The java code is below.
HttpSession session = request.getSession(true);
session.setAttribute("username", user);
session.setAttribute("id", id);
selected = new LessonSelection(id);
session.setAttribute("selected", selected);
dispatcher = this.getServletContext().getRequestDispatcher("/LessonTimetableView.jspx");
If you need any more of the code that I wrote, just ask thanks.
EDIT 1:
I thought I would check whether it is actually added to the session attribute and it is added. It is printed as 'model.LessonSelection#1457de3'. It still shows the same error.
The class at jsp:useBean must be the package + class of your attribute! Suppose your LessonSelection is located in the package com.test. The code would be as follow:
<jsp:useBean class="com.test.LessonSelection" id="timetable" scope="session"/>
Edit 1
Try this:
<jsp:useBean type="com.test.LessonSelection" id="timetable" scope="session"/>
It seens that using type, the JSP wont instantiate it for your, it'll just look from the bean of the given type in the given scope. See it here.
i want to use JSP values in servlet. iam fetching some parameters from the url and i need to pass those parameters in my servlet and also the servlet result something by using those parameters and i want to use the result again in the same jsp and in other jsp.
i get those parameters first time when my home jsp is called.
i am getting url parameter by using jstl core tags:
<c:set var="data" scope="session" value="${param.urldata}" />
How do i do this using JSP tags???
Thanks in advance
As i understand somewhat , you are trying to send the values from JSP page to Servlet..
For this we have lot of solution
Pass the value through method
Set the value at session and get it wherever you want in the whole application
I prefer second point, because which hold the value for the entire application
Check Pass variables from servlet to jsp
Above question pass the value from servlet to JSP,Your question is opposite of the answer that's it..!
Set value at JSP : session.setAttribute("key","value");
Get value at Servlet: session.getAttribute("name");
Also refer : Sharing values between servet and JSP
If I want to get the property from a bean and use it for some Java Code. Such as, get the visitor count but not output it, just for some other use. I cannot use because it'll output the value.
How can get the property but not output it?
You can set property to any variable then you can process it.
<c:set var="name" scope="scope" value="expression"/>
for example :
<c:set var="visitorCount" scope="scope" value="someBean.counter"/>
even you can process it without setting
This example retrieves a session-scoped bean and makes it available using the id myBean. Although it's not a good practice to have scriptlets in your jsp code, but one way is to call any method on the bean as shown below.
<jsp:useBean id="myBean" class="com.mycompany.MyBean" scope="session" />
<% myBean.getprop1(); %>
<jsp:useBean id ="object instance" class="full qualified path of ur class" scope="according to need"/>
<jsp:getProperty name="same as id" property="name of the property"/>
use according to this u must get ur result without using scripting....
I am using Servlet and JSP without a framework to study for my SCWCD. I have a simple form that I want the parameters to bind to a bean automatically. Is this possible without writing binding code or using a framework?
Thanks
Well, without a "framework" you can't do this. But you can use the Jakarta BeanUtils (http://commons.apache.org/beanutils/), more precisely the static method BeanUtils.populate in your servlet. Ex.:
BeanUtils.populate (myBean, request.getParameterMap());
Remember: the input properties names must match with bean attributes, ok?
You can do this with <jsp:useBean>.
<jsp:useBean id="form" class="com.example.Form" scope="request" />
<jsp:setProperty name="form" property="*" />
<jsp:include page="servletUrl" />
All bean properties whose names match the request parameter names -if any- will be set and the bean will be available as request attribute in the servlet matching the url-pattern of /servletUrl.
However, you'd like to use a servlet and/or MVC framework for this since it abstracts it all away and gives a better control over actions and response handling. This is essentially abuse of JSP (as being a view technology) as controller (which should be (in)directly done by a Servlet).
No, it isn't. You should use some framework, which I guess would be an overkill.
So what you can do, is iterate request.getParameterMap() keys and set the values to object with the corresponding field names (via reflection)
How do I pass a parameter from a page's useBean in JSP to a servlet in Java? I have some data in a form that gets passed no problem with a submit button, but no way to send anything else. Please help? Here is my code:
<input name = "deleteGameButton" type = "submit" value = "Delete"
onclick = "submitToServlet('DeleteGameServlet');">
Here is the corresponding javascript:
function submitToServlet(newAction)
{
document.userGameForm.action = newAction;
}
I'd like the servlet to have access to userBean
<jsp:useBean id = "userBean" scope = "session" class = "org.project.User" />
You kind of mess things here.
onclick() is Javascript and executed on client side. It has no (direct) way to update session-scoped bean. That bean is left on server-side, and was used when the HTML page was generated. To pass parameters back to servlet you need to use good old form fields, and submit the form.
Add more fields to the form, set their values before submit, then submit.
In Servlet call request.getParameter("name");
P.S. To automate this kind of things USE STRUTS. :-) Struts does exactly what you want: before passing the parameters to action, it populates the bean with those parameters. Transparently.
It depends exactly what you are trying to do. The
<jsp:useBean id = "userBean" scope = "session" class = "org.project.User" />
tag will allow you to use the userBean attribute of the session in your jsp. If there is not a userBean attribute in the session, it will create a new one (using the default constructor for org.project.User) and place it in the session.
Then, when you get to the servlet, you can retrieve it with:
User user = (User)request.getSession().getAttribute("userBean");
getServletConfig().getServletContext().getRequestDispatcher("servlet path & name");
dispatcher.forward (request, response);
Hi try with the next tag:
<jsp:useBean id = "userBean" scope = "session" class = "org.project.User"/>
<jsp:setProperty name="beanName" property="propertyname" value="value"/>
</jsp:useBean>
more here
Good luck!