
hi! i am developing web app of online exam system. I want to retrive question one by one as the user clicks "next/privious" button.I tried to get questions in arraylist in servlet and than set that arraylist as request attribute and than i wrote EL for that attribute on jsp page.But somehow i dont get any value in output, it gives nothing.What can be the problem.I dont want to use scriptlets.Pls help me to get this solved.
jsp:
<%--
Document : exam
Created on : 26 Feb, 2012, 9:20:08 AM
Author : Pratik
--%>
<%@page import="java.sql.ResultSet"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%@page import="dbconnection.Database" %>
<%! int i=1;%>
<form method="post" action=" ">
question is:${questionList[i]}
<tr><td><%=i%></td><td>${questionList[i]}</td>
<td><input type="radio" value=${Ans1["i"]} name="radio<%=i%>"${Ans1["i"]}</td>
<td><input type="radio" value=${Ans2["i"]} name="radio<%=i%>"${Ans2["i"]}</td>
<td><input type="radio" value=${Ans3["i"]} name="radio<%=i%>"${Ans3["i"]}</td>
<td><input type="radio" value=${Ans4["i"]} name="radio<%=i%>"${Ans4["i"]}</td>
</tr>
<%
i++;
//}
%>
<tr><td><input type="submit" value="Next"></td></tr>
</table>
</form>
</body>
</html>
Servlet Code:
package retrive;
import dbconnection.Database;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class retrive_que extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Database db = (Database) getServletContext().getAttribute("db");
ResultSet rs=null;
ArrayList questionList=new ArrayList();
ArrayList Ans1=new ArrayList();
ArrayList Ans2=new ArrayList();
ArrayList Ans3=new ArrayList();
ArrayList Ans4=new ArrayList();
String fetchque="select * from new_question order by que_no";
rs=db.runSql1(fetchque);
while(rs.next())
{
questionList.add(rs.getString(2));
Ans1.add(rs.getString(4));
Ans2.add(rs.getString(5));
Ans3.add(rs.getString(6));
Ans4.add(rs.getString(7));
}
request.setAttribute("questionList",questionList);
request.setAttribute("Ans1",Ans1);
request.setAttribute("Ans2",Ans2);
request.setAttribute("Ans3",Ans3);
request.setAttribute("Ans4",Ans4);
}
catch(SQLException e) {
out.close();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.