Servlet没有显示任何显示任何错误的内容

时间:2016-05-16 08:46:23

标签: java servlets

    <link rel="stylesheet" href="res/Site.css"/>



<body style="height: 800px">
     <pre>
   <!-- <h1 class="black"><center>Registration Detail<img src="res/login.png"/></center></h1>-->

    <h3>Cast Your Vote To The Right Persion</h3>    

    <form width="200" action ="regservlet" method="POST">

        <legend><i>Fill Up Your Details</i></legend>
        Voter Name     :<input placeholder="Enter Name" type="text" name="vname">

        Voter ID       :<input placeholder="Enter VoterID" type="text" name="vid">

        Email          :<input placeholder="Enter Email" type="text" name="vemail">

        Age            :<input placeholder="Enter Age(>18)" type="text" name="vage">

        Address        :<input placeholder="Enter Address" type ="text" name="vaddress">

        Pin Code       :<input placeholder="Enter PIN" type ="text" name="vpin">

        State          :<input placeholder="Enter State"type ="text" name="vstate">



    <input type="Submit" class="button Green" value="Submit"/>  <input type="Reset" class ="button red" value="Reset"/>
    </form>  
    <h4>Already Registered <a href="Login.html">Login Here</a></h4>
   <%-- ${requestScope.msg1}
    ${requestScope.msg2}
    ${requestScope.msg3}--%>
    </pre>


</body>

的servlet

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package web1;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jdbc.DAOLayer;

/**
 *
 * @author 1405075
 */
public class regservlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
         PrintWriter out = response.getWriter(); 

            String name=request.getParameter("vname");
        String id=request.getParameter("vid");
        String mail=request.getParameter("vemail");
        String age=request.getParameter("vage");
        String add=request.getParameter("vaddress");
        String pin=request.getParameter("vpin");
        String state=request.getParameter("vstate");
        String q = "select * from voter where vid ='"+id+"';";
        ResultSet rs = DAOLayer.selectData(q);

        try{
        if(rs.next())
        { RequestDispatcher rd = request.getRequestDispatcher("Registration.jsp");
        rd.include(request,response);
        out.println("<font color='Red'><h3>Please enter valid voter ID</h3></font><br>");
        out.println("<font color='Red'><h3>This Voter ID is already registered with us.</h3></font>");
        }


        else  

         //if(p.equals(cp))
            {
               String query = "insert into voter(vname,vid,vage,vaddress,vpin,vstate,vemail) values('"+name+"','"+id+"','"+age+"','"+add+"','"+pin+"','"+state+"','"+mail+");";
                int ur = DAOLayer.updateData(query);
                if(ur>0){
                 RequestDispatcher rd = request.getRequestDispatcher("Registration.jsp");
                  rd.include(request,response);
                  out.print("<h3><font color= 'Green'>Registered Successfully</font></h3>");
                }


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

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

的web.xml

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd“&GT;              Loginservlet         web1.Loginservlet                   Registrationservlet         web1.Registrationservlet     

<servlet>
    <servlet-name>regservlet</servlet-name>
    <servlet-class>web1.regservlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Registrationservlet</servlet-name>
    <url-pattern>/rs</url-pattern>
</servlet-mapping>



<servlet-mapping>
    <servlet-name>Loginservlet</servlet-name>
    <url-pattern>/Loginservlet</url-pattern>
</servlet-mapping>


<servlet-mapping>
    <servlet-name>regservlet</servlet-name>
    <url-pattern>/regservlet</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

enter image description here

在运行我的servlet时,它不会显示任何显示任何错误的内容。

1 个答案:

答案 0 :(得分:0)

你的做法是错误的。您将查询作为参数传递给DAOLayer。将查询作为String传递时,不会发送相应的变量值。它只是一个String =“select * from voter,其中vid ='”+ id +“'”; 。这里不发送varible id的值。而不是在DAOLayer中的方法中定义查询并在那里接收变量的值。正确的MVC方法是使用Beans。创建一个简单的注册Bean,如下所示: -

public class RegistrationBean {
   private String name;
   private String id;
   private String age;
   private String address;
   private String pin;
   private String state;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPin() {
        return pin;
    }

    public void setPin(String pin) {
        this.pin = pin;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

修改servlet代码以设置bean属性,如下所示: -

RegistrationBean rb=new RegistrationBean();
        rb.setName(name);
        rb.setId(id);
        rb.setAge(age);
        rb.setAddress(add);
        rb.setPin(pin);
        rb.setState(state);

要检查现有用户,请修改DAOLayer中的selectData方法以仅接收id并将其调用如下: -

ResultSet rs = DAOLayer.selectData(id);

使用rd.forward(request,response);代替rd.include(request,response);。执行此语句后,控制权将转移到Registration.jsp和这些语句: -

out.println("<font color='Red'><h3>Please enter valid voter ID</h3></font><br>");
        out.println("<font color='Red'><h3>This Voter ID is already registered with us.</h3></font>");

不会被执行。这是代码中的另一个错误。对于插入操作,修改updateData方法以接收RegistrationBean。使用bean的getter方法接收变量的值,如下所示: -

    public int updateData(RegistrationBean rb){
    String name=rb.getName();
    String id=rb.getId();
    String age=rb.getAge();
    String address=rb.getAddress();
    String pin=rb.getPin();
    String state=rb.getState();
       .
       . 
 rest of your code. Execute insert query here.

    }

这里将控件转移到Registration.jsp意味着更少。创建名为Success.html的HTML页面,该页面显示事务的成功消息。创建另一个名为Failure.Html的页面,该页面显示失败消息并将其命名为: -

if(ur>0){
              response.sendRedirect("success.html");
           }else{
              response.sendRedirect("failure.html");
           }

希望它会有所帮助。通过单击我答案左上角的右侧标记来标记问题已解决。如果没有,请告诉我。快乐编码:)