今晚在做练习的时候,需要用到从另外一个servlet钟传过来的参数,然后试了好多次,传过来的值都是空的,后来成功了。现在把解决办法贴出来,希望对大家有帮助。
第一个servlet,也就是要把值传出去的servlet,写以下语句:
String pageNow=(String)request.getAttribute("pageNow");
session.setAttribute("pageNow", pageNow);
第二个servlet,接受参数的那个:
HttpSession session = request.getSession(true)
String s_pageNow = (String) session.getAttribute("pageNow");
这样我们就可以直接在第二个servlet中取值了。
package servlet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.User;
import db.DB;
public class MainServlet extends HttpServlet {
// 重写init函数
public void init() {
try {
FileReader f = new FileReader("f:\\myCounter.txt");
BufferedReader br = new BufferedReader(f);
String numVal = br.readLine();
br.close();
this.getServletContext().setAttribute("visitTimes", numVal);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 重写destroy函数
public void destroy() {
try {
FileWriter fw = new FileWriter("f:\\myCounter.txt");
BufferedWriter bw = new BufferedWriter(fw);
bw.write(this.getServletContext().getAttribute("visitTimes").toString());
bw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setContentType("gb2312");
String username = request.getParameter("username");
String pwd = request.getParameter("pwd");
JspUserBeanCl ubc = new JspUserBeanCl();
DB db = new DB();
HttpSession session = request.getSession(true);
User user = new User();
session.setAttribute("user", user);
// 为AddServlet传一个用户
session.setAttribute("user", user);
if (user == null) {
user = db.checkUser(username, pwd);
}
// request.getSession().setAttribute("username", username);
if (user != null) {
ArrayList all = ubc.getUsersByPage(1);
int pageCount = ubc.getPageCount();
request.setAttribute("result", all);
request.setAttribute("pageCount", pageCount + "");
request.setAttribute("pageNow", "1");
第二个servlet:
package servlet;
import java.io.IOException;
import java.util.ArrayList;
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 JspUserClServelet extends HttpServlet {
public JspUserClServelet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 得到用户希望显示的pageNow