File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to perform MultiTask in a Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to perform MultiTask in a Servlet" Watch "How to perform MultiTask in a Servlet" New topic
Author

How to perform MultiTask in a Servlet

Swaminathan Balasubramani
Ranch Hand

Joined: Apr 12, 2005
Posts: 38
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());

}

}


}


B.Swaminathan<br /> <br />Mobile:+91 09886510453
Sarath Mohan
Ranch Hand

Joined: Mar 17, 2001
Posts: 213
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


Sarath Mohan
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Swaminathan Balasubramani
Ranch Hand

Joined: Apr 12, 2005
Posts: 38
hi ben and sarath

Thank u so much for the anticipated reply..

Thank you..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to perform MultiTask in a Servlet