Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Database values in JSP page  RSS feed

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a question regarding MVC and how data is transferred between servlets,Java Beans and jsp's. I have a JSP which has a drop down list to choose from. When the user makes thier choice, a servlet is called to process the request. Here is some code snipplet
import someCompany.someDept.NotesBean;
String usersChoiceOfDescription = req.getParameter("market");
String notesQuery="select fname from sgnotes";
String notesView ="/servlets/notes.jsp";
...
// Create the specific description Beans
NotesBean notesData = new NotesBean();
// opens database connections...
if(usersChoiceOfDescription.equals("notes"))
{
// out.println("Notes from SG");
rs = stmt.executeQuery(notesQuery);
getServletContext().setAttribute("notesData",notesData);
RequestDispatcher notesRd=req.getRequestDispatcher(notesView);
notesRd.forward(req,res);
}
//close DB connections...
Here is the JavaBean file
package someCompany.someDept;
import java.io.*;
import java.util.*;
// import java.beans.*;
public class NotesBean// JavaBeans example
{
private String fname;
private String lname;
private int age;
public void setFname (String fname)
{
this.fname=fname;
try
{
File file = new File("NotesBean.state");
FileOutputStream fos = new FileOutputStream( file);
fos.write( (this.fname).getBytes() );
fos.flush();
fos.close();
}
catch ( Throwable t )
{
t.printStackTrace();
}
}
public void setLname (String lname)
{
this.lname=lname;
}
public void setAge (int age)
{
this.age=age;
}
public String getFname()
{
return lname;
}
public String getLname()
{
return lname;
}
public int getAge()
{
return age;
}
}
Here is the JSP file which is supposed to show the output of 'fname' filed from the sgnotes table, in the 'fname' field.
<%@ page language="java" contentType="text/html" %>
<html>
<head>
<title>Choose market</title>
</head>
<body bgcolor="white">
<%@ include file="header.html" %>
<jsp:useBean
id="notesData"
class="someCompany.someDept.NotesBean"
scope="request">
</jsp:useBean>
<hr>
<h4>Choose a specific market from the following</h4>

<form method="get">
<select name="whichNote" size="5">
<br>
<option value="notesDescription"> <jsp:getProperty
name="notesData"
property="fname" />
<%-- gives an output of NULL instead of value of 'fname' from sgnotes table --%>

</select>
<br>
<input type="submit">
</form>
<hr>
<%@ include file="footer.html" %>
</body>
</html>
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// out.println("Notes from SG");
rs = stmt.executeQuery(notesQuery);
getServletContext().setAttribute("notesData",notesData);
you should be doing "request.setAttribute("notesData",notesData);
 
get schwifty. tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!