还是不行为什么就是没有执行呢?

还是不行为什么就是没有执行呢?

之前像老师问过。老师那里面是能执行的但是我这边还是不行,通过过滤器跳转后,依然没有执行servlet里面的内容

http://img1.sycdn.imooc.com//climg/5c077a6d000157ab10431079.jpg

付上代码

jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>


<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>注册界面</title>

</head>

<body>


<script type="text/javascript">

var email=<%=request.getParameter("email") %>

if(email==0){

alert("请输入正确的邮箱格式!")

}

var password=<%=request.getParameter("password") %>

if(password==0){

alert("密码不符合格式!")

}

var checkmima=<%=request.getParameter("checkmima") %>

if(checkmima==0){

alert("两次输入的密码格式不符合!")

}

var tel=<%=request.getParameter("tel") %>

if(tel==0){

alert("您输入的手机号不符合格式!")

}

</script>

<center>

<h1>用户注册</h1>

<form  method="post" action="<%=request.getContextPath() %>/Regist_servlet">

<table width="400px" cellspacing="0px" cellpadding="0px" border="1px">

<tr>

<td>用户名</td>

<td><input type="text" name="username" placeholder="用户名为3-12位字母数字或下划线组合" ></td>

</tr>

<tr>

<td>密&nbsp;码</td>

<td><input type="password" name="password" placeholder="密码长度为6-12位的纯数字"  id="password"></td>

</tr>

<tr>

<td>确认密码</td>

<td><input type="password" name="checkPWD" placeholder="密码长度为6-12位的纯数字" "></td>

</tr>

<tr>

<td>手机号码</td>

<td><input type="text" name="phone" placeholder="请输入正确的手机号码格式" ></td>

</tr>

<tr>

<td>邮箱</td>

<td><input type="email" name="email" placeholder="请输入正确邮箱格式" required="required"></td>

</tr>

<tr>

<td colspan="2" style="text-align:center">

<input type="submit" value="注册">

<input type="reset" value="重置">

</td>

</tr>

</table>

</form>

</center>

</body>

</html>

xml

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

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">

  <display-name>web_homework2</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

  

  

  

  <filter>

   <filter-name>login_filter</filter-name>

  <filter-class>com.filter.Login_filter</filter-class>

  </filter>

  <filter-mapping>

  <filter-name>login_filter</filter-name>

  <url-pattern>/Login_servlet</url-pattern>

  </filter-mapping>

  

  

  

  <filter>

  <filter-name>registfilter</filter-name>

  <filter-class>com.filter.Regist_filter</filter-class>

  </filter>

  <filter-mapping>

   <filter-name>registfilter</filter-name>

  <url-pattern>/Regist_servlet</url-pattern>

  </filter-mapping>

  

</web-app>

servlet

package com.servlet;


import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.lib.*;

import com.user.User;


@WebServlet("/Regist_servlet")

public class Regist_servlet extends HttpServlet {

private static final long serialVersionUID = 1L;


    public Regist_servlet() {

        super();

        // TODO Auto-generated constructor stub

    }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

Lib a=new Lib();

System.out.println("====进入Servlet");

HttpServletRequest htrequest=(HttpServletRequest)request;

User newuser=new User();

newuser.setUsername(htrequest.getParameter("username"));

newuser.setPassword(htrequest.getParameter("password"));

newuser.setEmail(htrequest.getParameter("email"));

newuser.setTel(htrequest.getParameter("phone"));

a.regist(newuser);//注册完毕

System.out.println("注册完毕");

response.sendRedirect(request.getContextPath()+"/index.jsp?flag=3");

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

System.out.println("====进入Servlet");

doGet(request, response);

}


}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,过滤器拦截了Regist_servlet没放行,所以不会执行servlet中的内容。同学需要在输出注册信息填写没有问题的代码后添加arg2.doFilter(htrequest,htresponse)使过滤器放行Regist_servlet。另外同学在Regist_filter中建议使用else-if的结构判断注册信息是否有误,避免出现重复提交的异常。代码如下:

http://img1.sycdn.imooc.com//climg/5c0792300001481909310500.jpg

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快!

  • 我叫飞天德 提问者 #1
    是说以后每次在过滤器结尾都最好要加arg2.doFilter(htrequest,htresponse)来放行一下吗?
    2018-12-05 16:58:20
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10205    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师