Tomcat出现404的解决方法[类型 状态报告 消息 请求的资源[/]不可用 描述 源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的资源]

我在这先说出我的例子
逻辑的开始是一个简单的jsp文件
一个from标签链接的是search文件,method属性是post代码如下

1
2
3
4
5
6

<body>
    <form action="search" method="post">
        姓名:<input name="name"/> 学号<input name="sno"/> 性别:<input nmae="gender"/>
        <input type="submit" value="搜索"/>
    </form>
</body>

之后根据网页的配置文件web.xml中查询search文件并点击搜索按钮的时候将数据传给该文件,并调用该servlet的post方法;

1
2
3
4
5
6
7
8
9
10
11
12
13
14

 <!--servletContext监听器-->
  <listener>
    <listener-class>jee.pk3.AppListener</listener-class>
  </listener>
 
  <servlet>
    <servlet-name>SearchServlet</servlet-name>
    <display-name>SearchServlet</display-name>
    <servlet-class>jee.pk3.SearchServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>SearchServlet</servlet-name>
    <url-pattern>/SearchServlet</url-pattern>
  </servlet-mapping>

上面代码中有一个servleContextLIstener监听器,该监听器的主要功能是当servletcontext容器调用时调用SqlSessionFactoryUtil.init();也就是当search文件的servletcontext容器调用的时候监听器就会调用SqlSessionFactoryUtil.init();

1
2
3
4
5
6
7
8
9
10
11
12
13
14

public class AppListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
       
        try {
            SqlSessionFactoryUtil.init();
        } catch (IOException e) {
           
            e.printStackTrace();
        }
    }

}

下面我们看看servlet文件中的代码。联系上面jsp文件中的代码以及web.xml代码可以看出search指的就是searchServlet文件,jsp文件传值也会传到该servlet文件的dopost方法的req参数中。然后借用req就可以调用jsp文件中的值了。
在下面就是mybatis的代码了,首先是实例化SqlSessionFactory ,该实例化涉及到刚才的配监听器,在调用servlet文件的时候servletcontext容器就会被调用,监听器的contextInitialized方法就会被调用,从而调用SqlSessionFactoryUtil.init();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

public class SearchServlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String name=req.getParameter("name");
        String sno=req.getParameter("sno");
        String gender=req.getParameter("gender");
        Student stu=new Student();
        stu.setName(name);
        stu.setSno(sno);
        stu.setGender(gender);

        SqlSessionFactory sqlSessionFactory=SqlSessionFactoryUtil.getInsertance();
        try (SqlSession sqlSession=sqlSessionFactory.openSession(true)){
            StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
            List<Student> list=studentMapper.search(stu);
            System.out.println(list);
        }
    }

}

下面是mybatis查询数据库的sqlSessionFactory 的实例化,也就是在servlet文件被调用的时候,该实例化就已经完成。这样书写的目的是保证sqlSessionFactory的单例,避免多次实例化sqlSessionFactory造成资源的浪费。

1
2
3
4
5
6
7
8
9
10
11
12
13
14

public class SqlSessionFactoryUtil {

    public static SqlSessionFactory sqlSessionFactory;
    public static void init() throws IOException {
        String config="mybatis_config.xml";
        InputStream is=Resources.getResourceAsStream(config);
        sqlSessionFactory =new SqlSessionFactoryBuilder().build(is);
    }
   
    public static SqlSessionFactory getInsertance() {
        return sqlSessionFactory;
    }
   
}

数据库的链接说完了,接下来我们说一说搜索语句的配置。首先我们要一个搜索方法的接口。接口代码如下:

1
2
3

public interface StudentMapper {
    public List<Student> search(Student stu);
}

下面是sql搜索的配置文件,首先使用mapper标签确定映射的接口;然后使用resultMap标签根据实体类将数据库中的字段映射到实体对应的属性上。最后调用select标签将接口中的方法设为id,parameterType属性设置参数方法的参数,resultMap属性设置的是映射关系。select内部是sql语句。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 
<mapper namespace="jee.pk3.StudentMapper">
<resultMap type="jee.pk3.Student" id="studentResultMap">
    <id property="id" column="S_id" ></id>
    <result property="name" column="s_name"/>
    <result property="sno" column="s_no"/>
    <result property="gender" column="s_gender"/>
   
</resultMap>
<select id="search" parameterType="jee.pk3.Student" resultMap="studentResultMap">
    select * from t_students where s_name=#{name}
</select>
</mapper>

好了我的分享到此结束,如果你的所有逻辑都能通顺该错误就不会出现,所以请你仔细检查自己的代码是否出现各个传递层次的断层。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地中海ing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值