wood burning stoves 2.0*
The moose likes Servlets and the fly likes Create servlet to display results set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Create servlet to display results set" Watch "Create servlet to display results set" New topic
Author

Create servlet to display results set

Curtis McBroom
Greenhorn

Joined: Sep 17, 2004
Posts: 1
I have a JSP that contains a simple form. Below I have posted my jsp and my servlet.

In my servlet I now just gather the data from my form and redisplay it on the screen (this was just my testing to see it work.)

Now what I am trying to do is have my servlet read from my MS SQL 2K database and display the information I desire. I am trying to display Last_Name, PRD, EAOS (all fields in my database). I would like the results to follow my example in my current servlet in that it creates the table columns on the fly vice having to build each one.

Any suggestions on how I can do this?



This is my jsp page that post to my servlet:

<%@ include file="personnel.inc.jsp" %>
<%!
public String removeNull(Object value) {
String ret = null;
if (value instanceof java.util.Date) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
ret = sdf.format(value);
}
else {
ret = (value == null)? "" : value.toString();
}
return ret;
}
%>
<%

// the date formatting:

SimpleDateFormat sdf_new = new SimpleDateFormat("MM/dd/yyyy");
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE,180);
String date = sdf_new.format(cal.getTime());
//String date = sdf_new.format(new Date());

// the username:
String username = System.getProperty( "user.name" );

%>

<html>
<head>
<title>Create PRD/EAOS Report</title>
</head>
<body>
<center>
<script src="../scripts/xaramenu.js"></script>
<script menumaker src="../scripts/clearlake7.js"></script>
</center>
<br>
<div align="center">
<font size="3"><b>CREATE PRD/EAOS REPORT</b></font>
<hr width="60%">
<form
name="Add_Member"
method="POST"
action="PRDReport">
<table border="1" cellpadding="2" cellspacing="0">
<%
//Create form to gather/update Prospective Gain information
ProsMember theMember = (ProsMember) request.getAttribute("prosmember");

if (theMember != null) {
%>
<tr>
<td class="tblHdr"><b>PRD</b></td>
<td class="tblHdr"><input type="text" value="<%= sdf_new.format(cal.getTime()) %>" name="PRD" size="12"></td>
<tr>
<td class="tblHdr"><b>Department</b></td>
<td class="tblHdr">
<select size="1" name="departmentid">
<optionvalue="<%= theMember.getDepartmentID() %>"><%= removeNull(theMember.getDepartment()) %></option>
<%
Vector theDeptlist = (Vector) BuildDropDown.getDepartments("department");

if (theDeptlist != null) {

for (int i=0; i < theDeptlist.size();i++) {
Drop_Down theDrop_Down = (Drop_Down) theDeptlist.elementAt(i);
%>

<option value="<%=theDrop_Down.getDepartmentID()%>"><%=theDrop_Down.getDepartmentName()%></option>

<%
} // end for
} // end if
%>
</select>
</td>
</tr>
<tr>
<td class="tblHdr"><b>Division</b></td>
<td>
<select size="1" name="divisionid">
<optionvalue="<%= theMember.getDivisionID() %>"><%= removeNull(theMember.getDivision()) %></option>
<%
Vector theDivlist = (Vector) BuildDropDown.getDivisions("division");

if (theDivlist != null) {

for (int i=0; i < theDivlist.size();i++) {
Drop_Down theDrop_Down = (Drop_Down) theDivlist.elementAt(i);
%>

<option value="<%=theDrop_Down.getDivisionID()%>"><%=theDrop_Down.getDivisionName()%></option>

<%
} // end for
}
%>
</select>
</td>
</tr>
<%
}
%>
</table>
</div>
<center><input type="submit" value="Submit" name="B1"></center>
</form>


</body>

</html>

--------

Here is my servlet:

package mil.navy.snadis.personnel.servlet;

/**
* @author McBroom, R. Curtis -- Predicate Logic, Inc.
*/
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/** Shows all the parameters sent to the servlet via either GET or POST.
* Specifically marks parameters that have no values or multiple values
*
* McBroom, R. Curtis -- Predicate Logic, Inc.
*/

public class PrdReport extends HttpServlet {

/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
String title = "Display PRD/EAOS Report";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=CENTER>" + title + "</H1>\n" +
"<TABLE BORDER=1 ALIGN=CENTER>\n" +
"<TR BGCOLOR=\"#FFAD00\">\n" +
"<TH>Parameter Name<TH>Parameter Value(s)");
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.print("<TR><TD>" + paramName + "\n<TD>");
String[] paramValues =
request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.println("<I>No Value</I>");
else
out.println(paramValue);
} else {
out.println("<UL>");
for(int i=0; i<paramValues.length; i++) {
out.println("<LI>" + paramValues[i]);
}
out.println("</UL>");
}
}
out.println("</TABLE>\n</BODY></HTML>");
}

public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}


RCMB
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
See the live demo at http://www.fdsapi.com


http://www.jamonapi.com/ - a fast, free open source performance tuning api.
JavaRanch Performance FAQ
Mahalkar Abhijeet
Greenhorn

Joined: Sep 20, 2004
Posts: 1
Hi this may not solve your problem. But i am giving a small code of creating tag library. which will help to generate and write the code which will give you the table creating just by passing resultset to the method.

thanks
Abhijeet MAHALKAR
SCJP.
SCWCD.



/*


After compiling this java file
create the .tld file "WEB-INF/RequiredtagLIB.tld" containing the following entries
MAKE SURE THAT FIRST FOUR LINE MUST BE AS IT IS OTHERWISE THROWS AN ERRORWHILE LOADING THE SERVER.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
<taglib>
<tlib-version>1.0</tlib-version> MANDATORY
<jsp-version>1.2</jsp-version> MANDATORY
<short-name>test</short-name> MANDATORY

<uri> http://www.manning.com/RequiredTagLIB </uri> AT MOST ONCE
<display-name> </display-name>AT MOST ONCE
<small-icon></small-icon>AT MOST ONCE
<large-icon></large-icon>AT MOST ONCE
<description></description>AT MOST ONCE
<validator></validator>AT MOST ONCE

<listener></listener>ANY NUMBER OF TIMES

<tag>AT LEAST ONCE
<name>required</name>MANDATORY
<tag-class> com.iflex.fcat.servlet.SCWCD.RequiredTag </tag-class> MANDATORY
AT MOST ONCE<tei-class> An optional subclass of javax.serlet.jsp.tagext.TagExtraInfo </tei-class>

<display-name> </display-name> AT MOST ONCE
<small-icon> </ small-icon> AT MOST ONCE
<large-icon> </large-icon>AT MOST ONCE
<variable> </variable>ANY NO. OF TIMES

<attribute>
<name></name>
<required>true/false </required>
<rtexprvalue>true/false</rtexprvalue> (Req time Expression )
<type>Data Type<type> only if above is true
<description> </description>
</attribute>ANY NO. OF TIMES

<example> <example>AT MOST ONCE
<body-content> empty / tagdependant / JSP (default) </body-content>AT MOST ONCE
<description>Prints the * whereever it occurs </description> AT MOST ONCE
</tag>
</taglib>

and put this file in WEB-INF/ directory
then make the following entry in WEB.xml file
<taglib>
<taglib-uri>
http://www.manning.com/RequiredTagLIB
</taglib-uri>

<taglib-location>
/WEB-INF/RequiredTagLIB.tld
</taglib-location>
</taglib>
then create the jsp file "EmptyTag.jsp"
<html><body>
<test:required /> This the demo of the sample TAG handler
</body></html>

NOTE: 1), 2) � are the LifeCycle methods.



TAG API
Tag (Interface)










IterationTag (Interface)






BodyTag ( Interface )TagSupport (Class)


BodyTagSupport (Class)
*/




package SCWCD ;
import javax.servlet.jsp.* ;
import javax.servlet.jsp.tagext.* ;
public class RequiredTag implements Tag{
private PageContext pageContext ;
private Tag parentTag ;

public void setPageContext(PageContext pageContext) {
this.pageContext = pageContext ;
}
public void setParent(Tag parentTag){
this.parentTag = parentTag ;
}

public Tag getParent(){
return this.parentTag ;
}

public int doStartTag() throws JspException{
try{

JspWriter out = pageContext.getOut() ;
out.println("<font color='#ff0000'>*</font>") ;
}catch(Exception e){
throw new JspException("Error in requiredtag.doStartTag()") ;
}
return SKIP_BODY ;
}
public int doEndTag() throws JspException{
return EVAL_PAGE ;
}
// clean up the resources (if Any )
public void release(){
}
}
[ September 20, 2004: Message edited by: Mahalkar Abhijeet ]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Originally posted by steve souza:
See the live demo at http://www.fdsapi.com


Now, how does blatantly promoting your product help answer his question? We do have a forum for blatant promoting.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
steve souza
Ranch Hand

Joined: Jun 26, 2002
Posts: 860
Well, I would argue that it isn't 'blatant advertising'? My interpretation of the original posters question...

Now what I am trying to do is have my servlet read from my MS SQL 2K database and display the information I desire. I am trying to display Last_Name, PRD, EAOS (all fields in my database). I would like the results to follow my example in my current servlet in that it creates the table columns on the fly vice having to build each one.

Any suggestions on how I can do this?


...is that he would like to display data from a database table, and not have to worry about how many columns are in the ResultSet and what there names are. In addition by looking at the code he is also interested in creating things like drop down list boxes. He asks for any suggestions on how he can do this. The live demo I pointed him to does this and also has has links to show all of the code, but I'm sure you looked and noticed that before typing your well thought out response. Certainly my suggestions was a lot better than you not suggesting anything.

At any rate for those that didn't look at the code, here is how you can do this. Not the only way mind you, but certainly a way:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create servlet to display results set
 
Similar Threads
form data
how to declare....and pass parameters..
Problem:The requested resource (/ShowParameters) is not available
Is this a limitation of JSP ?- I can do it in ASP
HTTP POST-Method not supported by this URL.