• Post Reply Bookmark Topic Watch Topic
  • New Topic

Database values in JSP page  RSS feed

 
Shuaib Gill
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>
 
trevershick
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);
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!