老师求救帮帮解决了,这两个问题真的解决不了了,找错找了大半天了
问题1: 编码格式已经全部设置为UTF-8之后,添加图书类别在下拉框里面还是乱码,在server的xml里面也设置了UTF-8也没有用,不知如何解决下面是有关的代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>图书添加</title> </head> <body> <center> <h1>图书添加</h1> <form action= /AddBookServlet" method="post"> <table width="400px" cellspacing="0px" cellpadding="0px" border="1px"> <tr> <td>图书ID</td> <td><input type="text" name="id" placeholder="请输入数字" pattern="\d+" required="required"></td> </tr> <tr> <td>图书名</td> <td><input type="text" name="bookName"></td> </tr> <tr> <td>图书分类</td> <td> <select name="catgoryName"> <c:forEach items = "${applicationScope.category}" var = "map"> <option value="${map.ID}">${map.CategoryName}</option> </c:forEach> </select> </td> </tr> <tr> <td>价格</td> <td><input type="text" name="price" placeholder="请输入价格" ></td> </tr> <tr> <td>描述</td> <td><input type="text" name="description" placeholder="请输入描述信息"></td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" value="添加"> <input type="reset" value="重置"> </td> </tr> </table> </form> </center> </body> <script type="text/javascript" src = "js/jquery-3.4.1.js"></script> <script type="text/javascript"> </script> </html>
package com.servlet; import java.io.IOException; import java.util.HashMap; import java.util.List; 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.main.LibDaoImpl; /** * Servlet implementation class CatgoryServlet */ @WebServlet("/CatgoryServlet") public class CatgoryServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public CatgoryServlet() { super(); // TODO Auto-generated constructor stub } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); Long idd = Long.parseLong(id); String category = request.getParameter("catgoryName"); String disc = request.getParameter("description"); LibDaoImpl ldd = new LibDaoImpl(); ldd.addBookCatgory(idd, category, disc); List<HashMap<String, Object>> mapp = ldd.getAllBookCatgory(); request.getServletContext().setAttribute("category", mapp); //String json = JSON.toJSONString(mapp); //response.setContentType("text/html;charset=utf-8"); //response.getWriter().println(json); //response.sendRedirect(request.getContextPath()+"/addBook.jsp"); response.setHeader("Content-type", "text/html;charset=UTF-8"); request.getRequestDispatcher("/catgory.jsp").forward(request,response); System.out.println(1); } }
问题二:在乱码没有解决的情况下,我继续进行一步的操作添加图书,可是会报404错误,我form提交的action明明是/AddBookServlet,404报错却显示是:Message /AddBookServlet%22 ;这个神奇的%22是什么东西呢???我完全没有写过这个,
求老师认真帮忙看看啊。。我看了一天了都不知道怎么回事,也clean了浏览器跟eclipse了也还是没用。。。心态炸了
0
收起
正在回答
2回答
同学你好!根据同学的代码乱码的原因是在添加分类时,接收到的为乱码,同学CatgoryServlet
中添加request.setCharacterEncoding(“utf-8”)
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
KelvinChung
2019-06-03 18:03:56
知道第二个为什么错了,,但是第一个乱码问题真的没办法了。。。求救啊老师
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星