AddCategoryServlet问题
1、提交页面是post方法,servlet中是不是代码只能写在dopost中?
2、下面两个跳转语句有什么区别喃,用第一个的话,页面没有显示
// response.sendRedirect(request.getContextPath() + "/categoryList.jsp");
request.getRequestDispatcher("/categoryList.jsp").forward(request, response);
3、使用第二个跳转页面,在页面上有显示,但是在地址栏显示地址是servlet,要是点击“分类管理”,之前提交的数据就会消失
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新建图书分类</title>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/add.css">
</head>
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href=""> 图书分类管理 </a>
</div>
</div>
</nav>
<div class="container">
<div class="jumbotron">
<h1>Hello, ${existUser.username}!</h1>
<p>请小心地新增图书分类,要是建了一个错误的就不好了。。。</p>
</div>
<div class="page-header">
<h3>
<small>新建</small>
</h3>
</div>
<form class="form-horizontal"
action="${pageContext.request.contextPath}/AddCategoryServlet"
method="post">
<div class="form-group">
<label for="categoryId" class="col-sm-2 control-label">分类ID
:</label>
<div class="col-sm-8">
<input name="categoryId" class="form-control" id="categoryId">
</div>
</div>
<div class="form-group">
<label for="categoryName" class="col-sm-2 control-label">分类名称
:</label>
<div class="col-sm-8">
<input name="categoryName" class="form-control" id="categoryName">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
</form>
</div>
<footer class="text-center"> copy@imooc </footer>
</body>
</html>
+++++++++++++++++++++++++++
package com.imooc.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.imooc.service.impl.CategoryServiceImpl;
@WebServlet("/AddCategoryServlet")
public class AddCategoryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 接收页面提交的图书类的ID-bookId,图书类的名字-categoryName
String categoryId = request.getParameter("categoryId");
String categoryName = request.getParameter("categoryName");
// 处理数据
CategoryServiceImpl category = new CategoryServiceImpl();
category.addCatgory(categoryId, categoryName);
request.setAttribute("categorys", category.getCategorydb());
System.out.println("保存书类的数据");
System.out.println(category.getCategorydb());
// 显示数据处理结果,跳转至categoryList.jsp
// response.sendRedirect(request.getContextPath() + "/categoryList.jsp");
request.getRequestDispatcher("/categoryList.jsp").forward(request, response);
doGet(request, response);
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星