• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Database values in JSP page

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic