Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to perform MultiTask in a Servlet

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everybody,

I'm Developing a maintenence application where i'l have to Add new user information and new Department information.

The HTML page consist of Few TextBoxes and Command buttons.

Acutally i i donno how to validate which command button has created an event, based on which i can execute the query either
for adding New User or New Department..

As i donno how to validate which component has created the event..i've written two servlet one for adding User and One for
adding Department, and have deployed it indivitually just for the sake to test whether the data are inserted..

I want to make it as a single servlet..Can anyone help me with the code pls...

I've attached the html code and The two servlet codes

pls help me!


The Html page consiste on Two text box and a Comand Button on one from for inserting Username and password into the database and
another form comsist of two textbox and a command button for adding new DeptName and Dept Description



SERVLET 1 ----For Inserting NEW USER INFORMATION
------------------------------------------------

import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.io.*;
import java.sql.*;

public class LoginSAPOnlineMaintenence extends HttpServlet
{

static Connection dbcon;

public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException
{

try
{

Class.forName("oracle.jdbc.driver.OracleDriver");

dbcon=DriverManager.getConnection("jdbc racle:thin:@bbsu-2:1521:keonline","system","knowledge");



System.out.println("Connection Established");

}

catch(ClassNotFoundException e)
{

System.out.println("Database Driver not Found");

System.out.println(e.toString());

}

catch(Exception e)
{

System.out.println(e.toString());

}

res.setContentType("text/html");

PrintWriter out=res.getWriter();

String username=req.getParameter("username");

String password=req.getParameter("password");

String previledge=req.getParameter("previledge");


int rows=0;

try

{

PreparedStatement s=dbcon.prepareStatement("insert into login_info(username,password,previledge) values(?,?,?)");

s.setString(1,username);

s.setString(2,password);

s.setString(3,previledge);

rows=s.executeUpdate();

}

catch(Exception e)

{

System.out.println(e.toString());

}


if(rows==0)
{

System.out.println("Error Inserting Data into the Login Table");

}

else

{

System.out.println("The values have been inserted into the table successfully");

}

try

{

dbcon.close();

}

catch(Exception e)

{

System.out.println(e.toString());

}

}


}



SERVLET 2 FOR ADDING NEW DEPT INFORMATION


import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.io.*;
import java.sql.*;

public class DepartmentSAPOnlineMaintenence extends HttpServlet
{

static Connection dbcon;

public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException
{

try
{

Class.forName("oracle.jdbc.driver.OracleDriver");

dbcon=DriverManager.getConnection("jdbc racle:thin:@bbsu-2:1521:keonline","system","knowledge");



System.out.println("Connection Established");

}

catch(ClassNotFoundException e)
{

System.out.println("Database Driver not Found");

System.out.println(e.toString());

}

catch(Exception e)
{

System.out.println(e.toString());

}

res.setContentType("text/html");

PrintWriter out=res.getWriter();

String deptid=req.getParameter("deptid");

String deptname=req.getParameter("deptname");

int rows=0;

try

{

PreparedStatement s=dbcon.prepareStatement("insert into dept_info(D_ID,D_DESC) values(?,?)");

s.setString(1,deptid);

s.setString(2,deptname);



rows=s.executeUpdate();

}

catch(Exception e)

{

System.out.println(e.toString());

}


if(rows==0)
{

System.out.println("Error Inserting Data into the Login Table");

}

else

{

System.out.println("The values have been inserted into the table successfully");

}

try

{

dbcon.close();

}

catch(Exception e)

{

System.out.println(e.toString());

}

}


}
 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't gone thru the entire code. Eventhough this is my idea.
Write the business logic in a single servlet and by passing hidden parameters from the form to identify the user action and invoke the appropriate methods in the servlet

Regards
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just test to see which button was pressed.
HTML Submit buttons send a name value pair just like a text field.



Then, in your servlet's doPost method:

[ April 21, 2005: Message edited by: Ben Souther ]
 
Swaminathan Balasubramani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ben and sarath

Thank u so much for the anticipated reply..

Thank you..
 
I've been selected to go to the moon! All thanks to this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic