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.
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 make a checklist webpage in jsp page. I am using struts2 tags in my jsp page. I want when my jsp first loaded the check box should automatically populated based on the value which comes from database. And when a user manually check or uncheck the check box something should get stored in the database, so that when another user access the same URL he can see the same state of check box. I don't want to use submit button.
Please help me how to implement it....!!!
Thanks in advance
Create String variables in your Struts2 Action files and also getters and setters. Set these variables in the execute() method (or the method you are using in your action class) with true or false values fetched from the database.
Then you can access them like this
<s:property value="varname"/>
You can set variables in your jsp like this
<s:set var="varname" value="varname"/>
Then check checkboxes like this
<input type="checkbox" <s:if test="#varname == 'true'">checked="checked"</s:if>/>
To set the values back to the database without submitting the form you will have to make an ajax call. Check out JQuery ajax() function. You will have to create a url with the parameters and values that will be mapped onto your corresponding variables in your action class. You can use these values in your action you specified in the url to write them to the database
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....
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 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!