两个servlet之间数值的传递

今晚在做练习的时候,需要用到从另外一个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");

String pageNow=(String)request.getAttribute("pageNow");
session.setAttribute("pageNow", pageNow);


String times = this.getServletContext().getAttribute("visitTimes").toString();
// 对times++再重新放回servlet
this.getServletContext().setAttribute("visitTimes", (Integer.parseInt(times) + 1) + "");
this.getServletContext().setAttribute("username", username);
// response.sendRedirect("main.jsp");
request.getRequestDispatcher("JspUserClServelet").forward(request, response);
} else {
response.sendRedirect("login.jsp");
}
}


}



第二个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

HttpSession session = request.getSession(true);


String s_pageNow = (String) session.getAttribute("pageNow");

// String s_pageNow = request.getParameter("1");
try {

int pageNow = Integer.parseInt(s_pageNow);// 这个可以合并成一个语句:int
// int pageNow = 1;
JspUserBeanCl ubc = new JspUserBeanCl();


ArrayList all = ubc.getUsersByPage(pageNow);
int pageCount = ubc.getPageCount();


session.setAttribute("result", all);
session.setAttribute("pageCount", pageCount + "");
// request.setAttribute("pageNow", pageNow + "");
session.setAttribute("pageNow", "1");
response.sendRedirect("main.jsp");
System.out.println("第二个");
// request.getRequestDispatcher("main.jsp").forward(request,
// response);
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}


public void init() throws ServletException {
}


}

你可能感兴趣的:(编程管理,两个servlet之间值得传递,Servlet,session)