我的表单上的通话字段

时间:2015-10-25 23:50:40

标签: java jsp servlets filter servlet-filters

我正在使用Java Web Applications,我有一个带过滤器的登录名(索引):

过滤器:

public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain)
        throws IOException, ServletException {

    if (debug) {
        log("Filtro:doFilter()");
    }

    doBeforeProcessing(request, response);

    Throwable problem = null;
    try {
              String usr = request.getParameter("usuario");
        String pswd = request.getParameter("password");
        if(usr == null && pswd == null){
             request.getRequestDispatcher("index.html").forward(request, response);
        }

        if(usr.equals("Cliente") && pswd.equals("123") ){
            request.getRequestDispatcher("/Cliente.jsp").forward(request, response);
       }else if (usr.equals("Supervisor") && pswd.equals("123")){ 
            request.getRequestDispatcher("/Supervisor.jsp").forward(request, response);
       }else if(usr.equals("Caja") && pswd.equals("123")){
            request.getRequestDispatcher("/Caja.jsp").forward(request, response);
       }else request.getRequestDispatcher("/unautorized.jsp").forward(request, response);


        chain.doFilter(request, response);

因此,当我以客户端身份登录时,它会将我重定向到登录视图,其中我有选择标记:

 <form method="POST">

  <select name="service">
        <option value="">Select an option</option>
        <option value="value1" name="sme">param1</option>
        <option value="value2" name="sma">param2</option>
        <option value="value3" name="ac">param3</option>
        <option value="value4" name="sm">param4</option>
        <option value="value5" name="sr"> param5</option>
        <option value="value6" name="sp">param6</option>
    </select>
    <input type="submit" value="Send">
</form>

我宣布变量:

 <%! 
    double price;
    %>

最后我使用Java根据select标签上的选定值打印一些值:

 <%
    String nuevo= request.getParameter("service");
    if(nuevo != null){
    String servm = request.getParameter("service");


    if (servm.equals("sme")) {
   price = 500.00;
   out.print("Cost service: " +price);
    } else if (servm.equals("sma")) {
        price = 1200.00;
        out.print("Cost service: "+price);

    } else if (servm.equals("ac")){
        price=800.00;
    out.print("Cost service:" +price);

    }else if(nuevo.equals("sm")){
    price=50.00;
    out.print("Cost service: " +price);
    }else if(nuevo.equals("sr")){
    price=75.00;
    out.print("Cost service: " +price);
    }else if(nuevo.equals("sp")){
    price=1500.00;
    out.print("Cost service: " +price);
    }
    }
    %>

的Servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String usr;
    String pswd;
    usr = request.getParameter("usuario");
    pswd = request.getParameter("password");
    request.getSession().setAttribute("usr", usr);
     RequestDispatcher rd = request.getRequestDispatcher("Resultado.jsp");
    rd.forward(request,response);
}

问题是,当我在表单按钮上单击“发送”时,它会将我重定向到登录(索引),因为我不会将用户和密码从过滤器调用到我的表单,我该怎么做? 另一个问题。就像Servlet不起作用...如果我删除了servlet的doPost代码,它只运行只验证我的过滤器,所以不需要它,这是正常的吗? 请帮忙。

0 个答案:

没有答案