当前位置: 代码迷 >> Java Web开发 >> 急jsp 从servlet 传值到jsp页面显示?乱码解决思路
  详细解决方案

急jsp 从servlet 传值到jsp页面显示?乱码解决思路

热度:356   发布时间:2016-04-17 10:21:56.0
急!jsp 从servlet 传值到jsp页面显示?乱码
[code=Java][/code]package book.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.util.List;

import javax.jms.Session;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import book.dao.BaseDao;
import book.entity.Book;

public class BookServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public BookServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
BaseDao b=new BaseDao();
response.setContentType("text/html");

String action=request.getParameter("action");
if(action!=null&&action.equals("showAll")){
List<Book> list=b.listAll();
request.setAttribute("booklist", list);
RequestDispatcher rd=request.getRequestDispatcher("show.jsp");
rd.forward(request, response);
}

if(action!=null&&action.equals("del")){
String bookID=request.getParameter("bookID");
int ac=Integer.parseInt(bookID);
int row=b.delBook(ac);

List<Book> list=b.listAll();
request.setAttribute("booklist", list);
RequestDispatcher rd=request.getRequestDispatcher("show.jsp");
rd.forward(request, response);
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// doGet(request,response);

}

public void init() throws ServletException {
// Put your code here
}

}
上面是SERVLET代码
-------------------------------
下面是显示代码
-------------------
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.util.*" %>
<%@page import="book.entity.Book" %>

  <% 
  //request.setCharacterEncoding("gbk");
  //new String(request.getParameterValues("checkbox1").getBytes("ISO-8859-1"),"UTF-8")
  List<Book> blist=(List<Book>)request.getAttribute("booklist");
  %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  
   
  <title>show.jsp</title>



  </head>
  
  <body>
  <table border="1" cellpadding="0" cellspacing="0"><caption>
  图书信息
  </caption>
 

  <tr>
  <td>图书编号</td>
  <td>图书名字</td>
  <td>图书价格</td>
  <td>操作</td>
  </tr>
  <%for(Book book:blist){ %>
  <tr>
  <td><%=book.getBookID() %></td>
  <td><%=book.getBookName() %></td>
  <td><%=book.getBookPrice() %></td>
  <td><a href="BookShow?action=del&bookID=<%=book.getBookID() %>">删除</a></td>
  </tr>
  相关解决方案