点击查询后,通过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" >医生姓名 :</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" >专业特长 :</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()+ " ");
}
%>
</div>
</div>
<%
}
%>
<div class="mt-4">
<span><input type="button" value="返回" onclick="history.back(-1)"/></span>
</div>
</div>
</div>
</body>
</html>