Calling java search function in JSP - java

I have a database setup with hibernate, and I want to call a search on the table in my JSP page. How to I call my java function with the jsp page and store the results, then print them on the jsp page?
Search function (Search by subject)
public Iterable<Layouts> getSelectedLayouts(String subjectName){}
I have the class containing this method in my jsp page as a spring bean
custom:useSpringBean var="layoutManager" bean="LayoutManager"
where LayoutManager is the class
I don't know how to call the method in the jsp page and output the results
I'm using a spring/hibernate to connect to the database, and running my jsp on a local tomcat server

Since it's not clear from your question as to what framework you're using. I will just give you a very general and simple answer but you can use the same idea in any framework you use.
You probably have a button in your jsp page which when clicked will invoke a servlet. Inside the servlet, you will have access to the fields that were submitted via the jsp page. Use the fields to create a search object(DAO) and query the database with it.
When you get results from the database, save it in session scope. So now in your jsp page, you can access the object and display it. You can use jstl to access the fields of the object.

Related

Update the database from submit option

I have a jsp page having a 'submit' option for input.On clicking it i want to update the value of a table called tbIndividual.What can be the best approach to do it?
On jsp page i have somthing like this :
User Name : <%=rs.getString(2)%>
First Name : <%=rs.getString(4)%>
Last Name : <%=rs.getString(5)%>
Email Id : <%=rs.getString(6)%>
Contact : <%=rs.getString(7)%>
<input type="submit" value="ADD"></input>
And now i want to update the value of status of that particular individual from 'NO' to 'WAIT' state.On click of this submit button.
Is making new servlet for this task a good option or doing the code in jsp a better one ?
If i need to make a new servlet then what will be the code for it on jsp page .?Please help.
If you are trying to learn servlet with this project then you should create a separate servlet where you will perform your business logic (e.g addition of element in Db) and jsp should be kept away from business logic because role of jsp is to render the output not to produce the output.
If this is a project for production purposes, then you should ( IMO you must ) opt some web framework. As framework will reduce your effort, headache and increase productivity.
First of all, there are certain things you need to understand while developing web applications in Java. I have a few recommendations and inputs
Please don't use Scriptlets. Scriptlets are deprecated and they make your code clumsy and the maintainance will be hard. Use JSTL
You need to create a form in your html to have a set of variables to push them to the server on clicking submit button. The form will have an action attribute which contains the URL where the request should be sent
Create a Servlet and map with the action URL and write the doPost method which can receive the form parameters and do the business logic whatever changing the status from NO to WAIT or anything else
Please take a note that you need to have Session variables in order to have the store the data in between requests from the same client. eg browser.
Is making new servlet for this task a good option or doing the code in jsp a better one ?
Writing a new servlet is a good option, than doing the business logic in jsp page. jsp is meant for presentation, just view. They shouldn't be used to perform business logic
If i need to make a new servlet then what will be the code for it on jsp page .?
JSP should just have all the necessary html elements and attributes like form, action attribute etc.. to post the parameters in the request to the action URL. Please note the method attribute of form. You should use HTTP POST method for posting form parameters to the server
Note : Finally, Writing Servlets are also NOT recommended. Instead, you should opt for webframeworks like Spring MVC , Struts etc. Please go through the link to understand about web frameworks answered by #BaluC
Hope this clarifies.

accessing variables from another form using javascript

in my current webpage (a JSP file which uses a form) i am interested in accessing variables that is autocalculated in the current webpage using a java class, from another form (also another JSP file) using javascript and DOM methods...is this possible?
Form A contains a link to Form B and i would like to put a javascript method in Form 2 to get variables/information in Form A.
EDIT 1: just updated...i'm trying at the moment to make context path URLs to each different web page and save them as strings in a database. And in the Form 2 webpage, i will retrieve the string URL context path of Form 1 from the database and then somehow try to access that...still figuring that part out..
EDIT 2: this is not involving Parent-Child window, but a relationship between 2 webpages that uses a FORM element

How to generate success alerts in jsp after successful execution of a servlet?

I have a jsp page which contains a form which calls a servlet, after inserting data into the database by a servlet hot to display successs alert to the user on the page?
Thanks in advance for answering.
Either you call the servlet asynchronously with AJAX and have a success callback function (see jQuery example: http://api.jquery.com/category/ajax/ )
Or you do it the old fashioned way with a synchronous Post which loads a success page, or the same page with for example a query parameter that states that you have succeed. This could then be used to activate a script block in your JSP (or whatever template language is your flavour).
You can use a javascript alert box. See http://www.w3schools.com/js/js_popup.asp

Calling a webpage from another webpage via jsp

I am trying to figure out how to do the following.
I have webpage at a certian location called www.hello.com/logout.jsp
What I am trying to do with logout.jsp is delete all the cookies that were stored when initially logging in. The problem is that there exists a cookie for a website with a different domain that is stored when logging in. The one way I can delete that cookie is through the logout link for that website e.g. www.hello2.com/logout.jsp
Is there anyway I can call www.hello2.com/logout.jsp from www.hello.com/logout.jsp?
I am trying to just make a call for www.hello2.com/logout.jsp from www.hello.com/logout.jsp and then redirect the user to another page on www.hello.com
Thanks in advance :D
If I understand correctly, you are trying to do an HTTP POST( or GET ) to www.hello2.com/logout.jsp while processing HTTP request to your web application's logout.jsp.
You should really consider coding your logic in Servlets and using JSPs only to present data, but in the meantime you can create a scriptlet inside your logout.jsp and do a call to another web page in there ( just don't code the whole thing in JSP, only make a call to a static method ).
In that static method you can use HttpClient to do whatever HTTP request you need from www.hello2.com.
Here are additions to your logout.jsp
<%# page import="my.package.Hello2Call" %>
<%
Hello2Call.postLogoutRequest( );
%>

hi i have a field called DECISION in my child jsp how can i get that value to parent jsp

hi i have 3 jsp pages 1.parent 2. child 3.grandChild
how can i access a radio button in grandChild jsp in Parent Jsp
can i access like this
parent.parent.document.forms['AccountClosureForm']['DECISION'][0]==true
I assume you either used page include directive or jsp include directive. I don't see any parent child relationship here. You would be able to share the whatever you have inside request or session or application scope. And I suppose you would have been using request, most probably.
From your code snippet, it looks you want to use it in JavaScript. Isn't it? If thats the case you can simply do something like document.forms["whatever"].... because its gonna be a single document on the client, no matter if you used a number of includes.
you cannot
JSP is run server side, you dont have a Javascript document on your server
If you want to have jsp 'talk' to each other you can use session and request objects
But once all 3 jsp are send to the browser and rendered as HTMl you can use any JavaScript you see fit to achieve what you want

Categories

Resources