Session attribute not working with useBean value - java

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.

Related

JavaBeans - getProperty to a variable

I create java class that include a list of users(called 'usersList' and include username and password for each value),
and now I create a JSP file and make scope of JavaBeans :
<jsp:useBean id="users" scope = "page" class="Package.VoteDB"> </jsp:useBean>
<jsp:getProperty name="users" property="usersList"/>
I want to get the usersList to a variable , and then check if username:admin , and password admin include in this list. with the current code, it's just Print the usersList to the screen. how can I save it in variable (without printing the list to the screen) and check if it's include the admin user?
thanks!!!
With <jsp:useBean>'s id value, you can refer to the the bean and its properties with EL.
${users.userList}
As for
check if it's include the admin user?
I wouldn't do this in a JSP. I would do it in a Servlet.

jsp:param with Java class

I have a JSP file that includes another JSP file. The first JSP should pass an instance of a Java class (widget) to the second JSP file.
This is what I have:
The first JSP:
<jsp:include page="/container/SpecialWidget.jsp">
<jsp:param name="widget" value="${widget}"/> // widget is a .Java POJO
</jsp:include>
The second JSP:
${param.widget.id}
The problem is that this code gives an error (it says it doesn't know ID). If I omit the ".id" part, the page prints the Java code for the Java class, which means the class has been transferred correctly. If I change the ${widget} rule of the first page in, for example, ${widget.id} and I try to print ${param.widget}, everything works fine.
My question: Why can't I pass a Java class and directly call upon its attributes? What am I doing wrong?
Edit: error message: Caused by: javax.el.PropertyNotFoundException: Property 'id' not found on type java.lang.String
I managed to fix my problem with the following code:
<c:set var="widget" value="${widget}" scope="request" />
<jsp:include page="/SOMEWHERE/SpecialWidget.jsp"/>
Thank you both for your help:) It saved my day
When you pass the variable ${widget} it is translated at request time to a string (widget.toString()). This value is then passed to the second JSP as a String, not as the original java object.
One approach to access the object's values is setting the parameter's value with the attribute's value:
<jsp:param name="widgetId" value="${widget.id}"/>
Then use the code bellow on the second JSP:
${param.widgetId}
You can also set widget as an request attribute and use it on the second page as ${widget.id} or ${request.widget.id}. I suggest you use the second approach.
<jsp:param> passes the parameter as an HTTP request parameter, which can only be a String. So toString() is called on your widget, and the result of this method is passed as parameter.
You should use a JSP tag, implemented as a tag file, instead of using a JSP include. See http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html for how to define an use them.
For example:
Tag definintion, in /WEB-INF/tags/specialWidget.tag:
<%# tag %>
<%# attribute name="widget" required="true" type="the.fully.qualified.name.of.WidgetClass" %>
TODO: add the HTML markup that must be displayed, using ${widget} to access the passed in widget attribute
Tag usage, in any JSP:
<%# taglib prefix="myTags" tagdir="/WEB-INF/tags" %>
...
Tada! I will use the specialWidget tag here, with widget as an attribute:
<myTags:specialWidget widget="${widget}"/>

Is there any other way to get property from bean in 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....

Using JSP Bean in a Servlet

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.

How to pass parameter to servlet

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!

Categories

Resources