Servlet output to JSP - java

I am writing an application in JAVA. Need advice about the menu.
I dont want to duplicate the code for menu creation in every JSP.
I want to create 1 JSP called menu.jsp and create the menu there and do a dynamic include <jsp:include... > wherever I need the menu.
This is fine till I have static menu.
Now, how would do do this for a dynamic menu.
I know it is a bad practice to connect to DB directly from JSP.
So what I want to do is this:
create a servlet
connect to DB and get the menu items and the display order
flush the menu created above to a jsp
So, my question is how do I include the above JSP wherever I need menu???

I think what you need is a templating system. Try out sitemesh for instance.
If you want to really use a java framework that helps you with good practices, i recommend to use grails

You may include servlet output by tag <jsp:include>. But it's a bad practice. See jsp tags instead

To use the menu, application wide put the generated menu either in session or application[ If and only if menu is same for every user]
Lets say menu.jsp has the code for generating the menu, get the session(or application object) and proceed with the requirement
Finally call menu.jsp using or <%#include >

Add the menu items, and a "renderMenu" boolean as request attributes.
Then include the menu.jsp and read those attributes - if renderMenu is false, don't output anything. If it is true - proceed with rendering the items (set by the servlet)

Related

Showing Database call result in Struts 2

Short Question, Is it possible to make a database call in the first load of home page using Struts 2? I have this old project of mine, just a simple online shop(wasn't able to finish it though) and i recalled that i was having a hard time with my home page outputting products listed in the database since struts needed to trigger an action before doing anything. Can you like preload some actions before proceeding to the home JSP? I'm a beginner in struts and could just do simple redirect and other basic actions.
You have many ways to do it. But most easiest way is mapping a action with the home page URL. Action class should do whatever database loading or other logic and forward to the JSP page.
Use a list method with s:iterator on your home.jsp.
You can use the s:action tag in your home page. There are 2 different ways it could be approached.
the only thing in your home page is the <s:action/> tag and let it render the result of the action: executeResult="true" attribute.
specify the var="myaction" attribute and set executeResult="false". You can then access everything from the action through the var name. for example <s:property value="%{myaction.myproperty}"/>
The docs for the s:action tag can be found here: https://struts.apache.org/docs/action.html

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.

how to redirect to another page in a common header

Currently I have three different apps deployed in the same AppServer. Their contexts are /appPortal, /appPortal/app1, /appPortal/app2
The app1 and app2 both have a common header which includes a jsp from appPortal like
<c:import url="/jsps/topHeader.jsp" context="/appPortal">
I tried to add some logic in this topHeader.jsp to redirect the user to another noaccess page who tried to access app1&app2 directly without login in the /appPortal but it doesn't work, and I don't want to add a filter to each application. What can I do, how can I make the redirection works?
Using a filter would be an ideal choice. But if you want to have it in JSP, try to keep the redirection logic before any HTML content is rendered.
"forward" and or "dispatcher". Those two words should be of use.

How to JSP display multiple page with one layout (Example index.jsp?page=about display about.jsp)

I new in JSP, i have a problem with JSP
in php i use
$page=$_GET["page"]
for display multiple page for one layout it mean i have index , it display layout and when i click on menu go to about us the index url = index.jsp?page=about
in PHP when i declare $page above and next step i do
Switch($page){
case 1:about
include 'aboutus.php'
case 2:news
include 'news.php'
}
How can i do it ?
How jsp can do the same way php to display multiple page in 1 layout
Use jsp:include.
<jsp:include page="/WEB-INF/${param.page}.jsp" />
And pass ?page=news or ?page=about, etc as parameter. The ${param.page} prints the outcome of request.getParameter("page"). You can prevent direct access to JSP files (by entering URL in browser address bar) by placing JSP files in /WEB-INF folder.
See also:
Basic JSP/Servlet tutorials
Hidden features of JSP/Servlet
How to avoid Java code in JSP
nowadays you use "templates" of Java Server Faces (JSF) for this approach. When you use JSP, you actually don't use the same concept as in PHP. You'd better use the MVC concept. But to answer your question, you could probably achieve this with the include tag http://java.sun.com/products/jsp/tags/11/syntaxref1112.html and control it with JSTL:
http://www.java2s.com/Code/Java/JSTL/JSTLiftag.htm

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