我有主页,当我点击参考Servlet不工作时我得到错误404.我认为问题在web.xml
映射,但不知道在哪里。请帮我纠正这个问题。谢谢。
我的web.xml
<!--Homepage.-->
<servlet>
<servlet-name>HomePageServlet</servlet-name>
<servlet-class>ru.pravvich.servlets.HomePageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HomePageServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--Add user in database.-->
<servlet>
<servlet-name>AddUserServlet</servlet-name>
<servlet-class>ru.pravvich.servlets.AddUserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddUserServlet</servlet-name>
<url-pattern>/addition</url-pattern>
</servlet-mapping>
我的jsp主页:
<body>
<ul>
<li><a href="addition.jsp">addition</a></li>
</ul>
</body>
使用doGet
方法的servlet:
public class HomePageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF8");
req.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(req,resp);
}
}
通过http://localhost:8080/items/
,我得到了我的主页。
但是,当我点击index.jsp
的引荐时,请返回:HTTP Status [404] – [Not Found]
addition.jsp
/WEB-INF/views/addition.jsp
同样谎言
我的Servlet用于addition.jsp
:
public class AddUserServlet extends HttpServlet {
private DBJoint db;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
db = (DBJoint) getServletContext().getAttribute("db");
db.getDBExecutor().addUser(
new User(req.getParameter("name"),
req.getParameter("login"),
req.getParameter("email")));
req.setAttribute("serverAnswer", "Add ok!");
req.getRequestDispatcher("/WEB-INF/views/answer.jsp").forward(req, resp);
}
}
addition.jsp
:
<body>
<form method="post" action="addition">
<input type="text" required placeholder="name" name="name"><br>
<input type="text" required placeholder="login" name="login"><br>
<input type="text" required placeholder="email" name="email"><br>
<input type="submit" value="add">
</form>
</body>
答案 0 :(得分:0)
我建议使用try/catch
和调试器模式。
尝试使用这样的getRequestDispatcher
request.getRequestDispatcher("answer.jsp").forward(request, response);
或
req.getRequestDispatcher("~/WEB-INF/views/answer.jsp").forward(req, resp);
我认为您需要获取每个参数的参数,而不是设置参数。试试这个;
public class AddUserServlet extends HttpServlet {
private DBJoint db;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
db = (DBJoint) getServletContext().getAttribute("db");
String Name = request.getParameter("name");
String Login= request.getParameter("login");
String Email= request.getParameter("email");
db.getDBExecutor().addUser(
new User(Name, Login, Email);
//And you need to 'serverAnswer' item in your 'answer.jsp' you know.
request.setAttribute("serverAnswer", "Add ok!");
request.getRequestDispatcher("answer.jsp").forward(req, resp);
}
}
然后您可以在getAttribute
,
answer.jsp
这样的内容
<%String Answer= (String)request.getAttribute("serverAnswer"); %><%= Answer%>
不要怪我,只是我想帮助你,我希望它对你有所帮助,如果你想要你可以看看我的试验项目; https://github.com/anymaa/GNOHesap
有一个很好的编码:)