Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to perform MultiTask in a Servlet

 
Swaminathan Balasubramani
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • 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());

}

}


}
 
Sarath Mohan
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • 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
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
hi ben and sarath

Thank u so much for the anticipated reply..

Thank you..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic