如何从html调用servlet文件

时间:2015-06-09 03:14:52

标签: java servlets

如何从html调用servlet文件。我试过但是当我点击提交它没有采取任何行动时,我也没有收到任何错误。我想在database.action事件中提交数据不起作用。请帮助我

HTML代码

    <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代码

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 个答案:

答案 0 :(得分:1)

将servlet中的processRequest(...){...}更改为doPost(...){...}

此链接可能会让您清楚。

ProcessRequest Method