This discussion is archived
2 Replies Latest reply: Oct 2, 2011 8:29 PM by 709779 RSS

servlet class file download

709779 Newbie
Currently Being Moderated
I have an HTML page , register.html. It calls a servelet 'Register.java'

The problem is that, the servlet displays a form window for user input. When user inputs data and submits, then a new window appears which asks us to download Register.class file.

Please suggest me how to fix this ??
  • 1. Re: servlet class file download
    EJP Guru
    Currently Being Moderated
    It calls a servlet 'Register.java'
    You mean it calls a service called ??? Which is implemented by Register.java?

    Show us some code.
  • 2. Re: servlet class file download
    709779 Newbie
    Currently Being Moderated
    EJP wrote:
    It calls a servlet 'Register.java'
    You mean it calls a service called ??? Which is implemented by Register.java?

    Show us some code.
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package defaultpack;
    
    import javax.servlet.*;
    import java.io.*;
    import java.sql.*;
    
    
    public class RegistrationServlet extends GenericServlet {
        
        private Connection con;
        private PreparedStatement ps = null;
        
        public void init() throws ServletException {
            System.out.println("In init");
            
            try {
                ServletContext ctxt = getServletContext();
                String driverClassName = ctxt.getInitParameter("driverClassName");
                Class.forName(driverClassName);
                String url = ctxt.getInitParameter("url");
                System.out.println(url);
                String dbuser = getInitParameter("dbuser");
                System.out.println(dbuser);
                String dbpass = getInitParameter("dbpass");
                System.out.println(dbpass);
                String sqlstmt = getInitParameter("sqlstmt");
                System.out.println(sqlstmt);
                con = DriverManager.getConnection(url,dbuser,dbpass);
                ps = con.prepareStatement(sqlstmt);
            }
            catch(Exception e) {
                e.printStackTrace();
                throw new ServletException("Initialization Failed, Unable to get DB Connection");
            }
        }
        
        
        public void service(ServletRequest req, ServletResponse res) 
                throws ServletException, IOException {
            
            System.out.println("In Service");
            res.setContentType("test/html");
            PrintWriter out = res.getWriter();
            
            out.println("<html><head>");
            out.println("<title>Tata Consultancy Services</title>");
            out.println("</head><body><h1>Tata Consultancy Services</h1>");
            
            out.println("<div>");
            out.println("<table border='1' width='100%' height='100%'>");
            out.println("<tr>");
            out.println("<td width='15%' valign='top' align='center'>");
            out.println("<br/><a href='login.html'>Login</a><br/>");
            out.println("<br/><a href='register.html'>Register</a><br/>");
            out.println("</td>");
            out.println("<td valign='top' align='center'><br/>");
                
            
            try {
                String uname = req.getParameter("uname");
                String pass = req.getParameter("pass");
                String repass = req.getParameter("repass");
                
                if(uname==null || uname.equals("")||
                        pass==null || pass.equals("")||
                        !repass.equals(uname)) {
                    out.println("<li><l>Given details are no valid. Please reenter</l></li>");
                }
            }
            catch(Exception e) {
                
            }
            
            out.println("</td>");
            out.println("</tr>");
            out.println("</table>");
            out.println("</div>");
            out.println("</body></html>");
            
        }
        
        public void destroy() {
            System.out.println("In destroy");
            try {
                con.close();
            }
            catch(Exception e) {
                e.printStackTrace();
            }
        }
    
    }
    Edited by: hagrid on Oct 2, 2011 2:00 PM

    Edited by: EJP on 3/10/2011 14:29: added {noformat}{cpde}{noformat} tags. Please use them.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points