寒湮. 2022-04-15 00:34 采纳率: 50%
浏览 153
已结题

添加@WebServlet注解后,执行跳转是一个空白页面。去掉后,报404错误,显示请求的资源[/VetServlet]不可用

点击查询后,通过VetServlet.java跳转到vetsearch_result.jsp。在添加@WebServlet注解后,能跳转,但却是一个空白页面。网上查资料有人说是注解的问题,但删掉注解后就不能正常跳转了显示请求的资源[/VetServlet]不可用。请问这个该怎样解决?
代码如下:
vetsearch.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>医生管理</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
</head>
<body>
    <div class="w-100 mt-5" style="min-width: 1200px;">
        <div class="w-100 text-center"><h2 style="color: #FBFCFC;">社区宠物诊所</h2></div>
        <div class="bg-primary d-flex justify-content-between">
            <div>
                <ul class="nav">
                    <li class="nav-item">
                        <a class="nav-link text-white" href="${pageContext.request.contextPath}/vetsearch.jsp">医生管理</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link text-white" href="${pageContext.request.contextPath}/custindex.jsp">客户管理</a>
                    </li>
                </ul>
            </div>
            <div>
                <ul class="nav">
                    <li class="nav-item"><a class="nav-link text-white" href="#">欢迎,<%= request.getParameter("name").toString() %></a></li>
                    <li class="nav-item"><a class="nav-link text-white" href="${pageContext.request.contextPath}/QuitServlet">退出</a></li>
                </ul>
            </div>
        </div>
        <div class="w-50 mt-4 text-center" style="margin-right:auto;margin-left:auto;">
            <form action="${pageContext.request.contextPath}/VetServlet" method="post">
                <div class="input-group mb-3 input-group-lg">
                    <div class="input-group-prepend">
                        <span class="input-group-text" >医生姓名&nbsp;:</span>
                    </div>
                    <input type="text" name="vetName" class="form-control" placeholder="请输入要查询的医生姓名">
                </div>
                <div class="input-group mb-3 input-group-lg">
                    <div class="input-group-prepend">
                        <span class="input-group-text" >专业特长&nbsp;:</span>
                    </div>
                    <input type="text" name="specName" class="form-control" placeholder="请输入要查询医生的专业特长">
                </div>
                <div class=" btn-group">
                    <button type="submit" class="btn btn-primary">查询</button>
                    <button type="button" class="btn btn-white "></button>
                    <button type="reset" class="btn btn-primary">重置</button>
                </div>
            </form>
        </div>
    </div>
</body>
</html>


VetServlet.java

package ph.servlet;

import ph.dao.VetDao;
import ph.entity.Vet;

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 java.io.IOException;
import java.util.List;


public class VetServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String m = request.getParameter("m");
        if("add".equals(m)){

        }else if ("search".equals("m")){
            search(request,response);
        }
    }
    private void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            String vetName = request.getParameter("vetName");
            String specName = request.getParameter("specName");
            VetDao vetDao = new VetDao();
            List<Vet> vets =vetDao.search(vetName,specName);
            if(vets.size() == 0) {
                request.setAttribute("msg","没找到相关医生信息!!");
                request.getRequestDispatcher("/vetsearch.jsp").forward(request,response);
            }else{
                request.setAttribute("vets",vets);
                request.getRequestDispatcher("/vetsearch_result.jsp").forward(request,response);
            }

        } catch (Exception e) {
            request.setAttribute("msg",e.getMessage());
            request.getRequestDispatcher("/vetsearch.jsp").forward(request,response);
        }
    }
}


vetsearch_result.jsp

<%@ page import="java.util.List" %>
<%@ page import="ph.entity.Vet" %>
<%@ page import="ph.entity.Speciality" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>医生查询结果</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
</head>
<body>
    <div class="w-100 mt-5" style="min-width: 1200px;">
        <div class="w-100 text-center"><h2 style="color: #FBFCFC;">社区宠物诊所</h2></div>
        <div class="w-50 mt-4 text-center" style="margin-right:auto;margin-left:auto;">
            <div class="row">
                <div class="col border py-1 border-right-0" style="background-color:#d5d7d9;border-color: #ffffff !important;">医生姓名</div>
                <div class="col border py-1" style="background-color:#d5d7d9;border-color: #ffffff !important;">专业特长</div>
            </div>
            <%
                List<Vet> vets = (List<Vet>)request.getAttribute("vets");
                for (Vet vet:vets ) {
            %>
            <div class="row">
                <div class="col py-2 border-right border-bottom" style="background-color: #FFFFFF;">
                        <%=vet.getName() %>
                </div>
                <div class="col py-2 border-bottom" style="background-color: #FFFFFF;">
                    <%
                        for (Speciality spec:vet.getSpecs()) {
                            out.print(spec.getName()+ "&nbsp;");
                        }
                    %>
                </div>
            </div>
            <%
                }
            %>
            <div class="mt-4">
                <span><input type="button" value="返回" onclick="history.back(-1)"/></span>
            </div>
        </div>
    </div>
</body>
</html>


  • 写回答

1条回答 默认 最新

  • 学习日记 后端领域新星创作者 2022-04-15 07:34
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月23日
  • 已采纳回答 4月15日
  • 创建了问题 4月15日

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?