重定向
AddItemServlet代码:
/** * */ package com.bjpowernode.drp.basedata.web; import java.io.IOException; import java.net.URLEncoder; import java.sql.Connection; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.bjpowernode.drp.basedata.domain.Item; import com.bjpowernode.drp.basedata.manager.ItemManager; import com.bjpowernode.drp.basedata.manager.ItemManagerImpl; import com.bjpowernode.drp.util.ApplicationException; import com.bjpowernode.drp.util.PageModel; import com.bjpowernode.drp.util.datadict.domain.ItemCategory; import com.bjpowernode.drp.util.datadict.domain.ItemUnit; /** * @ClassName:AddItemServlet * @Description:添加物料 * @author wm * @date 2016年1月27日下午7:49:19 */ public class AddItemServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //提交Servlet的时候防止乱码 request.setCharacterEncoding("GB18030"); //取得表单数据 String itemNo=request.getParameter("itemNo"); String itemName=request.getParameter("itemName"); String spec=request.getParameter("spec"); String pattern=request.getParameter("pattern"); String category=request.getParameter("category"); String unit=request.getParameter("unit"); //构造Item对象 Item item=new Item(); item.setItemNO(itemNo); item.setItemName(itemName); item.setSpec(spec); item.setPattern(pattern); //构造物料类别 ItemCategory itemCategory=new ItemCategory(); itemCategory.setId(category); item.setItemCategory(itemCategory); //构造物料单位 ItemUnit itemUnit=new ItemUnit(); itemUnit.setId(unit); item.setItemUnit(itemUnit); ItemManager itemManager=new ItemManagerImpl(); String errorMessage=""; try{ itemManager.addItem(item); }catch(ApplicationException e){ //request.setAttribute("error_message", "添加物料失败,物料代码【"+itemNo+ "】"); errorMessage ="添加物料失败,物料代码【"+ itemNo+"】"; } //重定向到查询页面 response.sendRedirect(request.getContextPath()+"/basedata/item_maint.jsp?errorMessage="+URLEncoder.encode(errorMessage,"GB18030")); //转发 //request.getRequestDispatcher("/basedata/item_maint.jsp").forward(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
html代码:
<font color="red"><%=request.getParameter("errorMessage")==null ? "":request.getParameter("errorMessage") %></font>
还需要修改tomcat配置,
右键tomcat服务--选中Browse deployment location
进入文件夹后,后退两层,选择conf
选择conf-server.xml
给端口8080的tomcat添加一条属性 URIEncoding="GB18030"
效果跟转发的效果是 一样的。
转发
AddItemServlet代码:
/** * */ package com.bjpowernode.drp.basedata.web; import java.io.IOException; import java.sql.Connection; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.bjpowernode.drp.basedata.domain.Item; import com.bjpowernode.drp.basedata.manager.ItemManager; import com.bjpowernode.drp.basedata.manager.ItemManagerImpl; import com.bjpowernode.drp.util.ApplicationException; import com.bjpowernode.drp.util.PageModel; import com.bjpowernode.drp.util.datadict.domain.ItemCategory; import com.bjpowernode.drp.util.datadict.domain.ItemUnit; /** * @ClassName:AddItemServlet * @Description:添加物料 * @author wm * @date 2016年1月27日下午7:49:19 */ public class AddItemServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //提交Servlet的时候防止乱码 request.setCharacterEncoding("GB18030"); //取得表单数据 String itemNo=request.getParameter("itemNo"); String itemName=request.getParameter("itemName"); String spec=request.getParameter("spec"); String pattern=request.getParameter("pattern"); String category=request.getParameter("category"); String unit=request.getParameter("unit"); //构造Item对象 Item item=new Item(); item.setItemNO(itemNo); item.setItemName(itemName); item.setSpec(spec); item.setPattern(pattern); //构造物料类别 ItemCategory itemCategory=new ItemCategory(); itemCategory.setId(category); item.setItemCategory(itemCategory); //构造物料单位 ItemUnit itemUnit=new ItemUnit(); itemUnit.setId(unit); item.setItemUnit(itemUnit); ItemManager itemManager=new ItemManagerImpl(); try{ itemManager.addItem(item); }catch(ApplicationException e){ request.setAttribute("error_message", "添加物料失败,物料代码【"+itemNo+ "】"); } //重定向到查询页面 // response.sendRedirect(request.getContextPath()+"/basedata/item_maint.jsp"); //转发 request.getRequestDispatcher("/basedata/item_maint.jsp").forward(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
HTML代码:
<td nowrap height="10" class="p3"> <font color="red"><%=request.getAttribute("error_message") %></font> </td>
效果: