正在回答
8回答
同学你好!
1.再检查一下页面的编码

祝学习愉快~
KelvinChung
2019-06-03 16:14:54
<%@ 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="BiggerProject/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.alibaba.fastjson.JSON;
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
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
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("/addBook.jsp").forward(request,response);
System.out.println(1);
}
}
KelvinChung
2019-06-03 14:57:59


老师设置了还是乱码哦!
KelvinChung
2019-06-03 12:28:58
KelvinChung
2019-06-03 12:13:47

就是箭头指的地方,用<option value="${map.get("ID")}">${map.get("CategoryName")}</option>这一句拿出map里面的值有点怪怪的,里面的元素应该是map类型存在list中,请问老师这里应该怎么改呢?添加之后是显示不出来下拉列表图书分类的值的
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10204 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程




恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星