participate


JavaServer Pages (JSP) and JSTL - JSP Edit Problem
<<   Back to Forum  |   Give us Feedback
10 Duke Stars available
This topic has 6 replies on 1 page.
sbucareer
Posts:53
Registered: 3/8/04
JSP Edit Problem   
Jul 26, 2004 7:08 AM

 
I have a JSP page that connect to backend via useBean. When I called the data from the backend i.e DataBase. I loop through my records and display them in HTML input value tag. The Idea was for someone to modify this record and send it Back to the backend as an update. I have tried so many tricks but could not get it to work.

Here is the frontend view in HTML


<%@ page import="java.sql., java.util., java.io." %>
<%@ page import="com.org.bugtracker.classes.BugtrackerDB"%>

<jsp:useBean id="demo" class="com.org.bugtracker.classes.BugtrackerDB"/>

<LINK rel="stylesheet" type="text/css" href="company.css" />
<SCRIPT type="text/javascript" src="company.js"></SCRIPT>

<%
String query = "SELECT
FROM inventory WHERE ip = '136.148.128.106'";


try{
if( !demo.isConnect( ) )
demo.connect( );

ResultSet rs = demo.viewQuery( query );

while ( rs.next( ) ){


%>
<HTML>
<HEAD></HEAD>
<BODY>
<FORM>
<!--<DIV STYLE="position:absolute; top:50px; left:100px;">-->
<TABLE BORDER=0 WIDTH=700 CELLSPACING=3 CELLPADDING=3 >


<TR>
<td >Department name</td>
<td><input type=text name="dept" value="<%=rs.getString( "dept")%>"</td>



<td>Username</td>
<td><input type=text name="username" value="<%=rs.getString( "username")%>"</td>
</TR>
<TR>

<td>Room and Building</td>
<td><input type=text name="room" value="<%=rs.getString( "room")%>"></td>



<td>Asset tag ID</td>
<td><input type=text name="asset" value="<%=rs.getString( "asset")%>"></td>

</TR>

<TR>
<td >Machine name</td>
<td><input type=text name="machine" value="<%=rs.getString( "machine")%>"></td>



<td>E-Net Address</td>
<td><input type=text name="enet" value="<%=rs.getString( "enet")%>"></td>
</TR>
<TR>

<td>Item Description</td>
<td><input type=text name="descp" value="<%=rs.getString( "descp")%>"></td>


<td>Manufacturer</td>
<td><input type=text name="manufacturer" value="<%=rs.getString( "manufacturer")%>"></td>

</TR>


<TR>
<td >Machine Model</td>
<td><input type=text name="model" value="<%=rs.getString( "model" )%>"></td>


<td>IP Address</td>
<td><input type=text name="ip" value="<%=rs.getString( "ip")%>"></td>

</TR>

<TR>
<TD>Serail Unit</TD>
<td><input type=text name="serial" value="<%=rs.getString( "serial" )%>"></td>
</TR>


<TR>

<td>Processor</td>
<td><input type=text name="processor" value="<%=rs.getString( "processor")%>"></td>


<td>Speed MHz</td>
<td><input type=text name="speed" value="<%=rs.getString( "speed" )%>"></td>

</TR>

<TR>

<td>Hard Drive Size (GB)</td>
<td><input type=text name="harddrive" value="<%=rs.getString( "harddrive" )%>"></td>
</TR>
<TR>






<td>RAM (MB)</td>
<td><input type=text name="ram" value="<%=rs.getString( "ram")%>"></td>

</TR>

<TR>

<td>CD-ROM</td>
<td><input type=text name="cdrom" value="<%=rs.getString( "cdrom" )%>"></td>

<td>ZIP</td>
<td><input type=text name="zip" value="<%=rs.getString( "zip" )%>"></td>

</TR>

<TR>

<td>OS</td>
<td><input type=text name="os" value="<%=rs.getString( "os" )%>"></td>


<td>Monitor Size</td>
<td><input type=text name="monitor" value="<%=rs.getString( "monitor")%>"></td>

</TR>

<TR>

<td>Classification</td>
<td><input type=text name="classification" value="<%=rs.getString( "classification" )%>"></td>


<td>CD-RW</td>
<td><input type=text name="cdrw" value="<%=rs.getString( "cdrw")%>"></td>

</TR>


<%

}//End while

}catch( Throwable ex ){

}//End
%>


<%demo.disconnect( ); %>

</TABLE>

<P><P>
<input type=hidden name=event value="TO_EDIT_INVENTORY">
<INPUT TYPE=SUBMIT class=button>
<INPUT TYPE=RESET class=button>

</DIV>
</FORM>
</BODY>
</HTML>
 
pgeuens
Posts:2,414
Registered: 22/08/01
Re: JSP Edit Problem   
Jul 26, 2004 7:15 AM (reply 1 of 6)  (In reply to original post )

 
you dont need "tricks" to program , just use the correct syntax.

To store info from HTML into JSP/servlet combination you need to do a submit, to do a submit you need an action in your form.

so first you need something like
<FORM name="chooseaname" method="post" action="urltothejsporservletthatwilldotheupdate" >
 
sbucareer
Posts:53
Registered: 3/8/04
Re: JSP Edit Problem   
Jul 26, 2004 3:46 PM (reply 2 of 6)  (In reply to #1 )

 
I have a servlet that listern to events within the container. When the submit button is click the container listern to these events. The rest work fine. I do not need a <FORM action> framework. My program is on MVC

Here is the servlet the controls all the event enerated by clicking a button. i.e

ResourceBundle bundle = ResourceBundle.getBundle( "event" );

Use Enumeration to get values and keys

This key and value are store in a properties file i.e To_EDIT_INVENTORY=com.org.bugtracker.event.ToEditInventory.java

A properties file mapped to URL i.e To_EDIT_INVENTORY=/jsp/toEditInventory.jsp

This is MVC. The rest of the file works. But to update this fields when they are called from database is the problem

The send button eventListener sends it to an event class that belongs to this action called ToEditInventory.java . In that class I have all the logic to update the database i.e

String query = "UPDATE inventory SET dept=?, room=?, ... WHERE ip=136.148.128.106

Mind you this is an internal LAN IP so you can not reach it (WARNING) You need to compromise the proxy server then compromise the ISA's, then compromise the Packet filtering ISA and them the almighty Millennium gatewatcher that runs on UNIX also act as a router filtering to the rest of the DSN Servers.

Anywaay sorry I wondered away. But when I click on the submit button the database do not update
 
phranquie
Posts:13
Registered: 6/20/03
Re: JSP Edit Problem   
Jul 26, 2004 5:13 PM (reply 3 of 6)  (In reply to #2 )

 
well, you have a <form> on the page... note, the person who responded was correct. Without an action attribute for the form, your page won't work. Now that 2 of us are telling you the same thing, will you listen?
 
phranquie
Posts:13
Registered: 6/20/03
Re: JSP Edit Problem   
Jul 26, 2004 5:17 PM (reply 4 of 6)  (In reply to original post )

 
and while I'm on a rant, don't go on and on about MVC when you have sql on a jsp. The "V" in MVC is View, and jsp is a technology primarily designed for view, not logic like sql. The results from the query are part of the "M", the model, and the "C", while I'm at it, is controller.
 
sbucareer
Posts:53
Registered: 3/8/04
Re: JSP Edit Problem   
Jul 27, 2004 1:58 AM (reply 5 of 6)  (In reply to #4 )

 
Thank you all for you input. I still don't want to use <form action> framework. Phranquie thanks for the MVC lecture. Below is my actual controller. Next after controller is Model, above is the View. I know I'm not suppose to use SQL in view but I did that when I got frustrated with the work. I just want is to work.


package com.org.bugtracker.servlets;

import com.org.bugtracker.event.EventBaseHandler;

import java.util.HashMap;
import java.io.IOException;
import java.util.Enumeration;
import java.util.ResourceBundle;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Controller extends HttpServlet{

protected static HashMap events = new HashMap( );

public void init ( ) throws ServletException{

ResourceBundle bundle = ResourceBundle.getBundle( "event" );

Enumeration e = bundle.getKeys( );
while( e.hasMoreElements( ) ){
String key = ( String ) e.nextElement( );
String value = bundle.getString( key );

try{
EventBaseHandler event = ( EventBaseHandler )
Class.forName( value ).newInstance( );
events.put( key, event );
}catch ( Exception exception ){
//We wiil try and do something here in the future
}//End exception
}//End while
}//End init method

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


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

String event = ( String ) this.validateEvent( request );
EventBaseHandler handler = getEventHandler ( event );

try{
handler.process ( request, response );
}catch ( Exception exception ){
request.setAttribute ( "error", exception );
handler = this.getEventHandler( "UNKNOWN_ERROR" );
}//End exception
handler.forward ( request, response );
}//End doPot Method


protected String validateEvent( HttpServletRequest req ){
String event = req.getParameter( "event" );
if( event == null || !events.containsKey( event ) ){
event = "UNKNOWN_EVENT";
}
return event;
}//End validate method

protected EventBaseHandler getEventHandler( String theHandler ){
EventBaseHandler handler;
try{
handler = ( EventBaseHandler ) events.get( theHandler );
}catch( Exception exception ){
handler = ( EventBaseHandler ) events.get( "UNKNOWN_EVENT" );
}//End exception
return handler;
}//End getEventHandler
}//End Controller class


This is the Model for this particular update records


package com.org.bugtracker.event;


import java.sql.;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import java.io.IOException;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.org.bugtracker.classes.BugtrackerDB;

public class ToEditInventoryEventHandler extends EventBaseHandler{

private ResourceBundle bundle = ResourceBundle.getBundle( "URL" );

protected String getURL(){
return bundle.getString( "TO_EDIT_INVENTORY" );
}//End getURL


public void process( HttpServletRequest request,
HttpServletResponse response )
throws IOException, ServletException{


BugtrackerDB demo = new BugtrackerDB( );


String dept = request.getParameter ( "dept" );
String username = request.getParameter ( "username" );
String room = request.getParameter ( "room" );
String asset = request.getParameter ( "asset" );
String machine = request.getParameter ( "machine" );
String enet = request.getParameter ( "enet" );
String descp = request.getParameter ( "descp" );
String manufacturer = request.getParameter ( "manufacturer" );
String model = request.getParameter ( "model" );
String ip = request.getParameter ( "ip" );
String serial = request.getParameter ( "serial" );
String processor = request.getParameter ( "processor" );
String speed = request.getParameter ( "speed" );
String harddrive = request.getParameter ( "harddrive" );
String ram = request.getParameter ( "ram" );
String cdrom = request.getParameter ( "cdrom" );
String zip = request.getParameter ( "zip" );
String os = request.getParameter ( "os" );
String monitor = request.getParameter ( "monitor" );
String classification = request.getParameter ( "classification" );
String cdrw = request.getParameter ( "cdrw" );



/*String query = "UPDATE inventory SET dept='"dept"', username='"username"',"
"room='"+room
"',asset='"asset"', machine='"machine"', enet='"enet"',"
"descp='"+descp
"', manufacturer='"manufacturer"', model='"model"',"
"ip='"+ip
"', serial='"serial"', processor='"processor","
"speed='"+speed
"', harddrive='"harddrive"', ram='"ram"',"
"cdrom='"+cdrom
"', zip='"zip"', os='"os"', monitor='"monitor"',"
"classification='"+classification
"', cdrw='"cdrw"' WHERE ip='136.148.128.106'";
/

String query = "UUPDATE inventory SET harddrive='10' WHERE ip='136.148.128.106'";

try{

if( !demo.isConnect( ) )
demo.connect( );

demo.executeQuery ( query );

}catch ( Throwable e ){

}//End exception

}//End process
}//End EventBaseHandler class



Look at it properly what I did was to have one controller for all my event either through <form action=controller Method=POST> or <form> send a hidden action called name=event value=TO_EDIT_INVENTORY they all do the same. Tell me otherwise

Thanks
 
omegaNetwork
Posts:281
Registered: 12/23/03
Re: JSP Edit Problem   
Jul 28, 2004 10:53 AM (reply 6 of 6)  (In reply to #5 )

 

String query = "UUPDATE inventory SET harddrive='10'
WHERE ip='136.148.128.106'";
First Valentine, I don't know if this is where the problem is, but I think you should look at the piece of code shown above. The update statement is not correct. It should be update, not uupdate.

Change that first and see if it works, otherwise let us know

See you Evans
 
This topic has 6 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 133

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums