Reputation: 7
I am comfortable with Java but I have no experience with JSP pages.
I have a simple login form on my index page which calls a Java class called LoginController to verify the details.
I don't know how to pass the information from LoginController to a new JSP page. I am sure this is a simple line of code but I can't find it.
Thanks
Upvotes: 0
Views: 2743
Reputation: 6208
Java Servlet has some scopes (I know 3). So you can add info to you jsp using info that contains in your scopes.
request scope Object assotiated with request. You can access info during session life.
request.setAttribute("attribute_name", result_from_java_class);
but sometimes you want to show some info on many pages so if you use request scope you need all time set this attribute.
but you can you session scope(assotiate object with the session). Now you can access to object during all you work(session closed when you close site).
session.setAttribute("attribute_name", result_from_java_class);
Context scope
A servlet can bind an object attribute into the context by name. Any attribute bound into a context is available to any other servlet that is part of the same Web application. it's also have the same methods
I hope this helps)
Upvotes: 0
Reputation: 20751
u can do all in jsp . use JAVA MVC Framework
even without that u can perform simple backhand operations using Servlet
or else you can use Java Beans which is simple business logic implementation
example for java beans please reffer http://www.tutorialspoint.com/jsp/jsp_java_beans.htm
example for Servlet is given below
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="add" method="post">
Value 1:<input type="text" name="val1" id="val1"/><br>
Value 2:<input type="text" name="val2" id="val2"/><br>
<input type="submit" value="Submit"/><br>
</form>
<%String sum="";
sum = (String)request.getAttribute("val3"); %>
<input type="text" value="<%=sum%>" />
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>add</servlet-name>
<servlet-class>controller.add</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>add</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
add.java
package controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class add extends HttpServlet {
String val3;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String val1=request.getParameter("val1");
String val2=request.getParameter("val2");
if(val1 != null && val2 != null)
val3=""+(Integer.parseInt(val1)+Integer.parseInt(val2));
else
val3="";
request.setAttribute("val3",val3);
request.getRequestDispatcher("index.jsp").forward(request, response);
try {
} finally {
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Upvotes: 0
Reputation: 1661
I have been using the below method, hope it helps. I normally call the other classes in my servlet and this acts as the controller connecting my java code (model) with the jsp (view), see the below code
request.setAttribute("attribute_name", result_from_java_class);
Then i create a dispatcher that sends the result to the jsp page :-
RequestDispatcher dispatcher = request.getRequestDispatcher("name_of_the_jsp_file+extension");
And finally i forward it:
dispatcher.forward(request, response);
On the jsp page to retrieve the result i.e information passed from the java class, will be as follows
request.getAttribute("attribute_name");
Hope it helps. Cheers.
Upvotes: 1