日期:2014-05-17  浏览次数:20814 次

jsp如何从servlet取值?
package org.entity;

public class Entity {
public Entity() {
}

private int bookid;
private int bookno;
private String bookname;
private String search;

public int getBookid() {
return bookid;
}

public void setBookid(int bookid) {
this.bookid = bookid;
}

public int getBookno() {
return bookno;
}

public void setBookno(int bookno) {
this.bookno = bookno;
}

public String getBookname() {
return bookname;
}

public void setBookname(String bookname) {
this.bookname = bookname;
}

public String getSearch() {
return search;
}

public void setSearch(String search) {
this.search = search;
}

}
package org.servlet;

import java.awt.List;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;

import org.DBconnection.DBconnection;
import org.entity.Entity;

import com.sun.corba.se.pept.transport.Connection;

import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class Servlet extends HttpServlet {

private static final HttpSession Session = null;

/**
* The doPost method of the servlet. <br>

* This method is called when a form has its tag value method equals to
* post.

* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* @throws Exception
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
String search = request.getParameter("search");
Entity entity = new Entity();
DBconnection dbconnection = new DBconnection();
try {
String sql = "select * from bookop where bookname = '" + search
+ " ' ";
ResultSet rs;
Statement stmt = null;
stmt = dbconnection.getDBconnection().createStatement();
rs = stmt.executeQuery(sql);
PrintWriter out = response.getWriter();
if (rs.next()) {
int bookid = rs.getInt("bookid");
int bookno = rs.getInt("bookno");
String bookname = rs.getString("bookname");
entity.setBookid(bookid);
entity.setBookno(bookno);
entity.setBookname(bookname);
request.getRequestDispatcher("/index.jsp").forward(request,
response);
} else {
request.getRequestDispatcher("/error.jsp").forward(request,
response);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}