java


Forward from JSP to another JSP without using a servlet between them?


I have a JSP page :
employee5_searchByName.jsp
<%#page import="java.util.Iterator"%>
<%#page import="java.util.List"%>
<!-- EMPLOYEE wants to search items by their name -->
<!DOCTYPE html>
<html>
<head>
<title>Employee - Search for items by name</title>
<link rel="stylesheet"
href="./css/styles.css"
type="text/css"/>
<link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" />
<script src="http://www.google.com/jsapi"></script>
<script>
google.load("jquery", "1");
</script>
<script src="js/jquery.autocomplete.js"></script>
<style>
input
{
font-size: 120%;
}
</style>
</head>
<body>
<h1>Employee - Search for items by name</h1>
<h1>
Search for items by name
</h1>
<fieldset>
<legend>Please enter the item's name :</legend>
<form action="Employee5_After">
Item's name : <input type="text" name="prod_name" id="myProduct"><br>
<script>
$("#myProduct").autocomplete("autocompleteFromDb.jsp");
</script>
<input type="submit" value="Register">
</form>
</fieldset>
</body></html>
It forwards to this servlet :
Employee5_After
package controller.employee;
/**
* Retrieve the record of a given product by its name
* using hibernate
* #author X
*
*/
#WebServlet("/Employee5_After")
public class Employee5_After extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// grab the product's name that the user entered
String productName = request.getParameter("prod_name");
// create DB instance
DatabaseInventory inventory = new DatabaseInventory();
// get the details
String details = inventory.getProductDetailsByName(productName);
HttpSession session = request.getSession();
// forward answer to JSP
synchronized(session)
{
if (details != null) // then the product has been found
{
session.setAttribute("foundProd", details);
String addressPath = "/WEB-INF/results/employee/employeeResult5.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(addressPath);
dispatcher.forward(request, response);
}
}
}
}
That servlet is doing his stuff , and then forwards to a 2nd JSP ,called :
employeeResult5.jsp
<!-- Employee - get a description of a product by its name -->
<!DOCTYPE html>
<html>
<head><title>The details for the product you requested are below</title>
<link rel="stylesheet"
href="./css/styles.css"
type="text/css"/>
</head>
<body>
<h1>Here are the details from the product you request :</h1>
<h2>${foundProd}</h2>
<h1>We wish you well - bye bye!</h1>
<fieldset>
<legend>Go back to Employee's web-page</legend>
<form action="blablabla">
Press here to continue
</form>
</fieldset>
</body></html>
I guess that I can use the <% and %> in the JSP to do the logic side of the servlet (contacting to DB and retrieve data) . How can I avoid using a servlet in between , and just pass the data from one JSP to another JSP ?
Separate the business logic from the font end, there is no need to redirect to an intermediate servlet. The best practice is to put the business logic in a separate class and instantiate that class in the destination page. Here is one example:
mainPage.jsp - create the page similar to your employee5_searchByName.jsp. Now this page posts the data you enter.
Create a backing class called - dbData.java (DatabaseInventory in your case)- put all your database query here and functions to retrieve what your want. A function like public String searchText(String param) (similar to getProductDetailsByName(productName)) which will essentially fetch your search results from database.
Now the most important part - Instantiate this class in your destination SearchResults.jsp page and show whatever data you get in a manner similar to this:
<%#page import="mysource.dbData"%>
<%
searchParam = request.getParameter("searchStr");
dbData data = new dbData();
String result = data.searchText(searchParam);
%>
<HTML>
<BODY>
The result is: <% out.print(result); %>
</BODY>
</HTML>
The industry standard is to follow an MVC Architecture. Following that will create applications which are clear to understand and easy to maintain.
You can use the request.redirect(URL) method to do it.
Or you can use request. forward(req, resp).
See example
Try this code to forward with parameteres
<jsp:forward page="URL">
<jsp:param nama="param1" value="hello"/>
<jsp:param nama="param2" value="hello2"/>
<jsp:param nama="param3" value="hello3"/>
<jsp:param nama="param4" value="hello4"/>
.
........... and so on
</jsp:forward

Related Links

Reversing a String using recursion in Java
(Java) Getting two threads to communicate with each other whilst running?
How to set FEATURE_SECURE_PROCESSING in XMLReaderFactory?
Output of the webdriver program is not coming in the right format(tabular way)
What does a type name in brackets mean?
PrimeFaces Component for Facebook-like “Notifications” (little messages in the upper right corner)
Calling another JFrame in Java
JSTL variables not being displayed
Exception in thread “Thread-2” java.lang.NullPointerException.error in my code
Spring-plugin build error
How to get the today's date / time using the TimePicker?
Call WebService method created with Netbeans and Glassfish from browser(localhost)
Spring using Anonymous Authentication Provider to use Guest User
Read data from JTable and save it to excel (java)
Java generics inheritance warning
Fast sort by date of huge file ArrayList

Categories

HOME
xamarin
ionic-framework
blogger
keycloak
tinymce
vmware
alfresco
mean-stack
adb
programming-languages
yahoo-oauth
amazon-ecs
youtube-api-v3
fsm
blueprintjs
qore
amazon-cloudformation
node-pdfkit
dax
event-handling
flask-wtforms
hapi
pc
pythonanywhere
realex-payments-api
django-storage
kendo-datasource
objectanimator
code-contracts
picasso
wpf-controls
dosbox
restlet
dartium
bitbucket-pipelines
lto
event-driven
serverside-rendering
sqlite2
jna
opshub
quadratic-programming
nand2tetris
unixodbc
acoustics
avconv
react-scripts
drupal-6
carrot
spring-mongodb
bind9
smart-table
infix-notation
radtreelist
underscore.js-templating
mikroc
py2app
fuzzy-search
gstreamer-0.10
connect-by
hill-climbing
hsv
wso2cloud
msys
intel-fortran
metaclass
angular-leaflet-directive
riak-cs
microbenchmark
processmodel
nsight
sortedlist
proj4js
javax.mail
android-imagebutton
neolane
jubula
jquery-layout
dealloc
balanced-payments
terminal-services
dotnetnuke-5
html4
padarn
ocunit
delphi-6
selected
viewswitcher
chrono
.nettiers
filtered-index
vc90
mtj
putchar

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile