Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need help on servlet

    Hi,

    i have developed a servlet java file for my webapplication. it is for storing the details in database and send a mail. But its not working.

    please chk the below code and advise if anything wrong in the code. also find the sql table format.

    Servlet code:
    Code:
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import javax.servlet.ServletContext;
    import datasource.dataSourcecon;
    import datasource.webSourcecon;
    import web.webservlet.MailerBean;
    import web.webvo.webQueryContVo;
    
    public class webQueryContDB {
        
        Connection con ;
        Connection webcon;
        PreparedStatement ps ;
        PreparedStatement ps1;
        PreparedStatement ps2;
        MailerBean mailob;
        MailerBean mailob2;
        dataSourcecon conobj=new dataSourcecon();
        webSourcecon webconobj=new webSourcecon();
        
        public webQueryContDB() {
        System.out.println("RequestDamageDB");
        }
    
         private  String timeStampTodate(String date) {
           java.text.SimpleDateFormat source = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            java.text.SimpleDateFormat target = new java.text.SimpleDateFormat("dd/MM/yyyy");
            System.out.println("Date :"+date);
            String date1=null;
            
            try  {
                if(date == null && date.equalsIgnoreCase("")) {
                    date="";
                }
                else if(date != null && !date.equalsIgnoreCase("")){
                    date1 = target.format(source.parse(date));
                    System.out.println("Date : "+date1);
                }
            }
            catch(Exception e) {
                System.out.println("excepion while formatting "+e);
            }
            return date1;
            
        }
        
         
        public boolean setRequest(webQueryContVo reqdamvo)
        {
            
            System.out.println("setRequest");
            
            boolean result  =false;
            
            con=webconobj.getconection();
            
            webcon=webconobj.getconection();
            
            System.out.println("Coonection done");
            String Message="";
           
            String mailid = "";
            String SqlReqmail = "select seq, email from web_contact_queries_emails where Dept = '"+reqdamvo.QueryType.trim()+"'";
            int seq = 1;
            String seqPrefix = "REQ";
            
            System.out.println("sql : "+SqlReqmail);
            try 
            {
            mailob=new MailerBean();
            mailob2=new MailerBean();
             } catch(Exception e){
                
                System.out.println("Exception at getting mail id : "+e);
                
            }
            
            System.out.println("aferMail");
            
            try 
            {
                ps =  con.prepareStatement( SqlReqmail);
                ResultSet rs = ps.executeQuery();
                System.out.println("try");
                 if(rs.next())
                 
                 {
                   mailid = rs.getString("email");
                   seq= rs.getInt("seq");
                 }
                
                 if (mailid == null || mailid.trim().equals(""))
                 {mailid = "admin@abc.com";}
                 
                reqdamvo.MailSent = mailid.trim();
                
                System.out.println("mailid : "+mailid);
                
            } catch(Exception e){
                
                System.out.println("Exception at getting mail id : "+e);
                
            }
            
        	try 
            {
            SqlReqmail = "update web_contact_queries_emails set seq = seq + 1";
            ps = con.prepareStatement(SqlReqmail);    
            int res1 =  ps.executeUpdate();
            
    		}catch(Exception e)
            {
                System.out.println("Exception at update web_contact_queries_emails"+e);
            }
            
    		
            
    		seqPrefix = String.valueOf(seq);
    		
    		for (int i = 0; i < (5 - seqPrefix.length()); i++) {
    				seqPrefix = "0" + seqPrefix;
    		}
    		
    		seqPrefix = "QRY" + seqPrefix ;
    		
            String  sqlReqDam = "insert into web_contact_queries(name,company,telephone,email,country,query,cntr_stat,comment_queries,mail_sendto,contacted_date,seq) values (?,?,?,?,?,?,?,?,?,getDate(),?)";
            
            
            try
            {
             
                ps =  con.prepareStatement(sqlReqDam);
            
                ps.setString(1, reqdamvo.Name);  
                ps.setString(2, reqdamvo.CompanyName);
                ps.setString(3, reqdamvo.Telephone);
                ps.setString(4, reqdamvo.email);
                ps.setString(5, reqdamvo.country);
                ps.setString(6, reqdamvo.QueryType);
                ps.setString(7, reqdamvo.ContainerRef);
                ps.setString(8, reqdamvo.Query);
                ps.setString(9, reqdamvo.MailSent);
                ps.setString(10, seqPrefix);
    
                 int s =  ps.executeUpdate();
                 System.out.println("value of s :"+s);
                    if(s == 1)
                    {
                        result = true;
                    }else
                    {
                        result = false;
                    }
                    ps.close();
                    
                    Message = "";
                    Message = Message + "<html><head><LINK href='css/style.css' type=text/css rel=stylesheet></head>";
    	        Message = Message + "<style type='text/css'> .searchtxtbox2 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 9px; font-style: normal;	font-weight: normal; color: #000000; text-transform: uppercase; } .searchlabel { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; font-style: normal; font-weight: bold; text-transform: capitalize; color: #000000; } .txtbox2 { font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; font-style: normal; font-weight: normal; color: #000000; text-transform: uppercase; } </style>";
                    Message = Message + "<table width='770'  bgcolor='#FFFFFF'  border='0' cellpadding='1' cellspacing='1'>";
                    Message = Message + "<tr><td class='searchlabel'>From      : </td><td class='txtbox2'>" + reqdamvo.Name + "</td></tr>";
                    Message = Message + "<tr><td class='searchlabel'>Company   : </td><td class='txtbox2'>" + reqdamvo.CompanyName + "</td></tr>";
                    Message = Message + "<tr><td class='searchlabel'>Telephone : </td><td class='txtbox2'>" + reqdamvo.Telephone + "</td></tr>";
                    Message = Message + "<tr><td class='searchlabel'>Email : </td><td class='txtbox2'>" + reqdamvo.email + "</td></tr>";
                    Message = Message + "<tr><td class='searchlabel'>Country : </td><td class='txtbox2'>" + reqdamvo.country + "</td></tr>";
                    Message = Message + "<tr><td class='searchlabel'>QueryType : </td><td class='txtbox2'>" + reqdamvo.QueryType + "</td></tr>";
                    Message = Message + "<tr><td class='searchlabel'>Reference No : </td><td class='txtbox2'>" + reqdamvo.ContainerRef + "</td></tr>";
                    Message = Message + "<tr><td class='searchlabel'>Query : </td><td class='txtbox2'>" + reqdamvo.Query+ "</td></tr>";
                    Message = Message + "</table>";
                    Message = Message + "</html>";
            mailob.setMessage(Message.trim());
            mailob.setFrom(reqdamvo.email);   
            System.out.println("After calling of SetFrom");
            mailob.setTo(reqdamvo.MailSent);
           	mailob.setSubject(seqPrefix +" Enquiry From Web :"+reqdamvo.QueryType);
    
            try{
                mailob.sendMail();
                
            }
            catch(Exception e){
                System.out.println("Error at mailing"+e);
            }                
             try{
                mailob2.sendMail();  
                
            }
            catch(Exception e){
                System.out.println("Error at mailing"+e);
            }    
                  
                     
            }catch(Exception e)
            {
                System.out.println("Exception at setRequest"+e);
            }
            
                return result;
                
        }
          
    }

    SQL Table created for storing the detalis:
    Code:
    dbo.web_contact_queries
    columns:
               name (varchar(100),null)
               company (varchar(100),null)
               telephone (varchar(100),null)
               email (varchar(100),null)
               country (varchar(100),null)
               query_country (varchar(100),null)
               seq (varchar(100),null)
               comment_queries (varchar(100),null)
               mailsendto (varchar(100),null)
               contacted_date (datetime,null)
               id (PK, int not null)
               cntr_stat (varchar(100),null)
               origin_country (varchar(100),null)

    Kindly help.

    Regards.. ganny.

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    As far as servlets go, this doesnt look like one. For a servlet you need to extend HTTPServlet and override the doGet and doPost methods to handle your http requests.

  • #3
    New Coder
    Join Date
    Aug 2009
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    actually it is a java file which is writing the data into database and the actual servlet is passing the values to this file through webvo file. pls find the below code and kindly assist.

    actual servlet:

    Code:
    import javax.servlet.ServletConfig;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpServlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import javax.servlet.ServletException;
    import web.webdb.webQueryContDB;
    
    public class webQueryContServlet extends HttpServlet {
    
        Connection reqCon;
    
        public void init(ServletConfig config) throws ServletException {
    
    
            super.init(config);
            System.out.println("Init method in RequestDamageServlet");
    
        }
    
        public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
            try {
                String ContactorName = request.getParameter("txtName");
                String ContactorCompany = request.getParameter("txtCompany");
                String ContactorTelephone = request.getParameter("txtTelephone");
                String ContactorEmail = request.getParameter("txtEmail");
                String ContactorCountry = request.getParameter("txtCountry");
                String ContactorDept = request.getParameter("txtDept");
                String ContactorContainerRef = request.getParameter("txtNumber");
                String ContactorQuery = request.getParameter("txtQueries");
                String ContactDate = request.getParameter("txtCurrDate");
    
                System.out.println("ContactorName : " + ContactorName);
                System.out.println("ContactorCompany : " + ContactorCompany);
                System.out.println("ContactorTelephone : " + ContactorTelephone);
                System.out.println("ContactorEmail : " + ContactorEmail);
                System.out.println("ContactorCountry : " + ContactorCountry);
                System.out.println("ContactorDept : " + ContactorDept);
                System.out.println("ContactorContainerRef : " + ContactorContainerRef);
                System.out.println("ContactorQuery : " + ContactorQuery);
                System.out.println("ContactDate : " + ContactDate);
    
                //assign to vo
                web.webvo.webQueryContVo queryvo = new web.webvo.webQueryContVo();
    
                queryvo.Name = ContactorName;
                queryvo.CompanyName = ContactorCompany;
                queryvo.Telephone = ContactorTelephone;
                queryvo.email = ContactorEmail;
                queryvo.country = ContactorCountry;
                queryvo.QueryType = ContactorDept;
                queryvo.ContainerRef = ContactorContainerRef;
                queryvo.Query = ContactorQuery;
                queryvo.CurDate = ContactDate;
    
                System.out.println("below getParameterNames at the Data r");
    
                System.out.println("DB--1");
                webQueryContDB reqdb = new webQueryContDB();
                System.out.println("DB--2");
                System.out.println("test");
                boolean ret = reqdb.setRequest(queryvo);
    
                System.out.println("DB--3");
    
                PrintWriter out = response.getWriter();
                out.write(String.valueOf(ret));
    
            } catch (Exception e) {
                System.out.println("Exception at getParameterValues" + e);
            }
    
    
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            processRequest(request, response);
        }
    
        private String checknull(String value) {
            String retvalue;
            if (value != null) {
    
                retvalue = value.toUpperCase().trim();
                return retvalue;
            } else {
    
                return retvalue = "";
            }
        }
    }

  • #4
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    By not working, what do you mean? Do you get any errors, anything printed out in logs or console? How about a stack trace?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •