Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Make servlet thread safe.

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Make servlet thread safe.

    Dear All,

    I have created a servlet that does authentication here is the following code
     
    import java.io.IOException;
     
    import java.io.PrintWriter;
     
    import java.sql.Connection;
     
    import java.sql.DriverManager;
     
    import java.util.Enumeration;
     
    import javax.faces.context.FacesContext;
     
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    import oracle.jdbc.pool.OracleDataSource;
     
    public class Authentication extends HttpServlet {
        public void doGet(HttpServletRequest request,
                          HttpServletResponse response) throws ServletException,
                                                               IOException {
            PrintWriter pw=response.getWriter();
            pw.write("<b>Authenticating please wait.....</b>");
            pw.flush();
            pw.close();
            Enumeration enu = request.getParameterNames();
            String userID = request.getParameter("userid");
            String password = request.getParameter("password");
            String port = request.getParameter("port");
            String database = request.getParameter("database");
            String server = request.getParameter("server");
     
            OracleDataSource ds;
            try {
     
                Class.forName("oracle.jdbc.OracleDriver");
                Connection con =DriverManager.getConnection("jdbc:oracle:oci8:@"+database,
                                                userID, password);
                System.out.println("Authentication Sucess := "+con.isValid(1));
                response.sendRedirect(request.getContextPath() +
                                      "/faces/template.jsp");
            } catch (Exception e) {
                System.out.println("Authentication failed");
                response.sendRedirect(request.getContextPath() +
                                      "/faces/authfailed.jsp");
            }
        }
    }

    I can make servlet to implement SingleThreadModel but it is not recommended.So how can i make my code to be thread safe..should i make doGet method to be synchronized ???

    -Thanks


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,225
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Make servlet thread safe.

    Why do you need to make it thread safe? The container should take care of all threading issues and it is not apparent you are starting any threads in this snippet of code.

Similar Threads

  1. What are the requirements to develop Servlet Application?
    By yousef atya in forum Java Servlet
    Replies: 2
    Last Post: July 28th, 2011, 06:20 PM
  2. jsp- servlet problems
    By kundan_101 in forum Java Servlet
    Replies: 1
    Last Post: December 31st, 2010, 04:42 PM
  3. Servlet or Jsp?
    By flangofas in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: December 8th, 2010, 10:54 AM
  4. New Window from a Servlet
    By RookieServletPrgmer in forum Java Servlet
    Replies: 6
    Last Post: December 2nd, 2010, 07:52 AM
  5. DateFormat is not thread safe
    By trueacumen in forum Threads
    Replies: 5
    Last Post: August 15th, 2009, 02:16 AM