How to call servlet file from html

1.3k views Asked by At

How to call servlet file from html.I tried but when i click on submit its not taking any action,I am not getting any error also. and i want to submit data in database.action event is not working.Please help me

HTML code

    <form name="form" method="post" action="NewServlet" >
    <label for='name' ><font size="2">Your Full Name*: </label><br/>
    <input type='text' name='name' id='name'  maxlength="50" style="height:30px; width :250;"  placeholder="Enter Full Name"/><br/><br/>


    <label for='email' >Email Address*:</label><br/>
    <input type='text' name='email' id='email' maxlength="50" style="height:30px; width :250;"  placeholder="Enter your Email"/><br/><br/>



    <label for='phone' >Phone Number*:</label><br/>
    <input type='text' name='phone' id='phone'  maxlength="15"  style="height:30px; width :250;"  placeholder="Enter Phone Number"/><br/><br/>

<label for='Reason' >Reason*:</label><br/>
<select  name="reason" style="height:30px; width :250;">
    <option>Select</option>
    <option>Enquiry</option>
    <option>Complain</option>
    <option>Order</option>
</select>
</br>
</br>
</br>

    <label for='message' >Address Or Message:</label><br/>

    <textarea style="height:100px; width :400;" name='message' id='message' placeholder="Enter  Address or Message"></textarea></p>
</b>
</size>

<%--
<input type='submit' name='btnSubmit'  value="Submit"/>
--%>
<img src="Image/submit1.png" style="width:150px; height:70px;top:50%px; " onmouseover="this.src='Image/submit2.png'" onmouseout="this.src='Image/submit1.png'"/>


</form>

Servlet code

public class NewServlet extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String driver= "com.microsoft.sqlserver.jdbc.SQLServerDriver";
       String url="jdbc:sqlserver://localhost:1433;databaseName=wristwatch; username=sa; password=abc@123;";


        try {
            //int id=Integer.parseInt(request.getParameter("txt_id"));
          String nm=request.getParameter("name");
          String email=request.getParameter("email");
           int phone=Integer.parseInt(request.getParameter("phone"));
            String reason=request.getParameter("reason");
             String add=request.getParameter("Address");


            Class.forName(driver);
          Connection c=DriverManager.getConnection(url);



          out.println("Data Inserted");  

        } 
        catch(Exception e)
        {
        System.out.print(e);


        }


        finally {            
            out.close();
        }
    }

web.xml

<servlet> 
   <servlet-name>NewServlet</servlet-name> 
   <servlet-class>NewServlet</servlet-class> 
   </servlet> 
  <servlet-mapping> 
     <servlet-name>NewServlet</servlet-name> 
     <url-pattern>/NewServlet</url-pattern> 
</servlet-mapping>
1

There are 1 answers

0
SASIKUMAR SENTHILNATHAN On

Change the processRequest(...){...} to doPost(...){...} in servlet.

This link might make you clear.

ProcessRequest Method