我正在制作一个关于在线测试的项目。 当我第一次运行该项目时,它可以正常工作,但是在第二次运行我的项目时,控件不会从我的servlet页面传输到JSP页面。
我认为requset.setAttribute(" ","");
无效。
我还尝试重置request,resetBuffer。
ServletTest2.java
public class ServletTest2 extends HttpServlet {
ArrayList sno=new ArrayList();
ArrayList question=new ArrayList();
ArrayList cans=new ArrayList();
ArrayList op1=new ArrayList();
ArrayList op2=new ArrayList();
ArrayList op3=new ArrayList();
ResultSet rs;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("in ServletTest");
try{
sno.clear();
question.clear();
cans.clear();
op1.clear();
op2.clear();
op3.clear();
System.out.println("in process request");
stmt=con.prepareStatement("select * from question order by rand() limit 3");
// stmt.setInt(1,i);
rs= stmt.executeQuery();
while(rs.next())
{
sno.add(rs.getInt(1));
question.add(rs.getString(2));
cans.add(rs.getString(3));
op1.add(rs.getString(4));
op2.add(rs.getString(5));
op3.add(rs.getString(6));
//System.out.println(question);
}
System.out.println(request);
sendToJsp(request,response); // User-define method which send value to JSP page
}
catch(Exception e){ }
}
}
static int i;
int ans;
public void sendToJsp(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{ System.out.println("Before request Attribute");
request.setAttribute("sno", sno.get(i).toString());
request.setAttribute("question", question.get(i).toString());
request.setAttribute("cans", cans.get(i).toString());
request.setAttribute("op1", op1.get(i).toString());
request.setAttribute("op2", op2.get(i).toString());
request.setAttribute("op3", op3.get(i).toString());
System.out.println("After request Attribute");
// response.flushBuffer();
//request.getServletContext().getRequestDispatcher("/hello.jsp");
RequestDispatcher rd=getServletContext().getRequestDispatcher("/dataTest3.jsp");
rd.forward(request, response);
}
int marks;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
System.out.println("In GET Method");
String answer=request.getParameter("option");
if(cans.get(i).equals(answer))
{
System.out.println("yes ,,,, Get it");
marks+=10;
}
else
{
marks-=10;
}
/*n++; System.out.println("n : "+n);
if(n==5)
{
request.setAttribute("marks",""+marks);
RequestDispatcher rd=request.getRequestDispatcher("/finalSubmit.jsp");
rd.forward(request, response);
}*/
//System.out.println("no of rows : "+(k+1));
System.out.println("Marks : "+marks);
i++;
if(i==sno.size())
{
//rs.close();
//stmt.close();
//
// stmt1.close();
System.out.println("in close");
con.close();
request.setAttribute("marks",""+marks);
RequestDispatcher rd=request.getRequestDispatcher("/finalSubmit.jsp");
rd.forward(request, response);
}
sendToJsp(request,response);
//System.out.println(answer);
//processRequest(request, response);
}catch(Exception e){ }
}
static int k,n=0;
String Canswer[]=new String[10];
Connection con=null;
PreparedStatement stmt=null;
Statement stmt1=null;
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{ System.out.println("In POST Method");
Class.forName("com.mysql.jdbc.Driver");
con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","");
processRequest(request, response);
}catch(Exception e){ }
}
}