WCD-LAB @HOME 10
You need to develop a Web page for the OniineAptitudeTest Web
application. This Web page should allow the administrator of the application to
add aptitude questions to the table in the database, as shown in the following
figure.
As shown in the preceding figure, each aptitude question has four options. When the user clicks the SUBMIT button, the aptitude question along with its four options should be inserted in the database. How will you develop this Web page?
1. Create a database. OnlineTest. with username, app. and password, app. in the JavaDb database.
2. Create a table. AptitudeQuestions. inside the OnlineTest database using the following query:
CREATE TABLE AptitudeQuestions
(
questionID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1),
questionStem varchar(1000),
optionl varchar(1000) ,
option2 varchar(1000) ,
option3 varchar(1000) ,
option4 varchar(1000) ,
answer varchar(1000)) ;
3. Open the Glassfish admin console.
4. Create a JDBC connection pool with values as shown in the following table.
Name Value
Pool Name OnlineTestPool
Resource Type javax. sql.DalaSo urce
Database Driver Vendor Derby-30
URL jdbc:derby://localhost:1527/OnlineTest
User app
DatabaseName OnlineTest
Password app
ServerName localhost
PortNumber 1527
Connection Pool Values
5. Create a JDBC resource. jdbc/OnlineTest. and map it to the connection pool. OnlineTestPool.
6. Create a Web application. OnlineTest.
7. Create a JSP page. QuestionPage.jsp. and replace the existing code with the following code:
<%6page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<title>Question Page</title>
</head>
<body>
<form action="AddQuestionServlet">
<table>
<tr><td>Question stem</td></tr>
<tr><td colspan="2"><textarea name="qStem" rows="10" cols="50"></textarea></td></tr>
<tr><td><input type="radio" name="options" value="optionl">Optionl</td></tr>
<tr><td><textarea name="textOptionl" rows="4" cols="50"></textarea></td></tr>
<tr><td><input type="radio" name="options" value="option2">Option2</td></tr>
<tr><td><textarea name="textOption2" rows="4" cols="50"></textarea></td></tr>
<tr><td><input type="radio" name="options"
value="option3">Cption3</td></tr>
<tr><td><textarea name="textOption3" rows=n4" cols="50"></textarea></td></tr>
<tr><td><input type="radio" name="options" value="option4">Cption4</td></tr>
<tr><td><textarea name="textOption4" rows=n4" col3="50"></textarea></td></tr>
<tr><td colspan="2" align="center"Xinput type="submit" value="SUBMIT">
</table>
</form>
</body>
</html>
8. Create a JSP page. Success.jsp. and replace the existing code with the following code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<title>Success Page</title>
</head>
<body>
<hl>Question Updated</hl>
<a href="QuestionPage.jsp">Add more questions</a> </body>
</html>
9. Create a servlet. AddQuestionServlet.java. and replace the existing code with the following code:
import ]ava.10.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import j ava.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import j avax.naming.InitialContext;
import j avax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import j avax.servlet.annotation.WebServlet;
import j avax.servlet.http.HttpServlet;
import j avax.servlet.http.HttpServletRequest;
import j avax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
SWebServlet(name = "AddQuestionServlet", urlPatterns : {"/AddQuestionServlet"})
public class AddQuestionServlet extends HttpServlet { DataSource ds;
Context context;
Connection con;
Statement st;
ResultSet rs;
PreparedStatement ps;
String insertQuery="Insert into AptitudeQuestions
(questionStem,optionl,option2,option3,option4,answer)values
/ O O O O O O \ •
/ /
String
qStem,optionl,option2,option3,option4,answer,correctCption; public AddQuestionServlet() {
try {
context = new InitialContext(); if (context == null) {
throw new RuntimeException("JNDI context
not found");
}
ds = (DataSource) context.lookup ("jdbc/OnlineTest");
if (ds == null) {
throw new RuntimeException("Datasource not
found");
}
con = ds.getConnection() ; st = con.createStatement(); ps=con.prepareStatement(insertQuery);
} catch (Exception e) {
System.out.println(e);
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8") PrintWriter out = response.getWriter(); try {
qStem=request.getParameter("qStem"); optionl=request. getParameter ("textcjptionl") ; option2=request.getParameter("textOption2"); option3=request.getParameter("textCption3"); option4=request.getParameter("textCption4"); correctCption=request.getParameter("options"); System.out.println(qStem+" "+optionl+" +option2+" "+option3+" "+option4+" "+correctOption); if(correctOption.equals("optionl")){
answer=request.getParameter("textCptionl");
}
else if(correctOption.equals("option2")){
answer=request.getParameter("textOption2");
}
else if(correctOption.equals("option3")){
answer=request.getParameter("textOption3");
}
else if(correctOption.equals("option4")){
answer=request.getParameter("textOption4");
}
else {
System.out.println("No answer is correct");
}
System.out.println(qStem+" "+optionl+" +option2+" "+option3+" "+option4+" "+answer);
ps.setstring(1, qStem); ps.setstring(2, optionl); ps.setstring(3, option2); ps.setstring(4, option3); ps.setstring(5, option4); ps.setstring(6, answer); int i=ps.executeUpdate();
System.out.println(i);
RequestDispatcher
rd=request.getRequestDispatcher("Success.j sp"); rd.forward(request, response); ps.close(); con.close(); context.close();
} catch (Exception e) {
System.out.println(e) ;
} finally {
out.close();
}
}
©Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { processRequest(request, response);
}
©Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { processRequest(request, response);
}
©Override
public String getServletlnfo() {
return "Short description";
}
}
10. Run the QuestionPage.jsp file.
As shown in the preceding figure, each aptitude question has four options. When the user clicks the SUBMIT button, the aptitude question along with its four options should be inserted in the database. How will you develop this Web page?
1. Create a database. OnlineTest. with username, app. and password, app. in the JavaDb database.
2. Create a table. AptitudeQuestions. inside the OnlineTest database using the following query:
CREATE TABLE AptitudeQuestions
(
questionID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1),
questionStem varchar(1000),
optionl varchar(1000) ,
option2 varchar(1000) ,
option3 varchar(1000) ,
option4 varchar(1000) ,
answer varchar(1000)) ;
3. Open the Glassfish admin console.
4. Create a JDBC connection pool with values as shown in the following table.
Name Value
Pool Name OnlineTestPool
Resource Type javax. sql.DalaSo urce
Database Driver Vendor Derby-30
URL jdbc:derby://localhost:1527/OnlineTest
User app
DatabaseName OnlineTest
Password app
ServerName localhost
PortNumber 1527
Connection Pool Values
5. Create a JDBC resource. jdbc/OnlineTest. and map it to the connection pool. OnlineTestPool.
6. Create a Web application. OnlineTest.
7. Create a JSP page. QuestionPage.jsp. and replace the existing code with the following code:
<%6page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<title>Question Page</title>
</head>
<body>
<form action="AddQuestionServlet">
<table>
<tr><td>Question stem</td></tr>
<tr><td colspan="2"><textarea name="qStem" rows="10" cols="50"></textarea></td></tr>
<tr><td><input type="radio" name="options" value="optionl">Optionl</td></tr>
<tr><td><textarea name="textOptionl" rows="4" cols="50"></textarea></td></tr>
<tr><td><input type="radio" name="options" value="option2">Option2</td></tr>
<tr><td><textarea name="textOption2" rows="4" cols="50"></textarea></td></tr>
<tr><td><input type="radio" name="options"
value="option3">Cption3</td></tr>
<tr><td><textarea name="textOption3" rows=n4" cols="50"></textarea></td></tr>
<tr><td><input type="radio" name="options" value="option4">Cption4</td></tr>
<tr><td><textarea name="textOption4" rows=n4" col3="50"></textarea></td></tr>
<tr><td colspan="2" align="center"Xinput type="submit" value="SUBMIT">
</table>
</form>
</body>
</html>
8. Create a JSP page. Success.jsp. and replace the existing code with the following code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html charset=UTF-8">
<title>Success Page</title>
</head>
<body>
<hl>Question Updated</hl>
<a href="QuestionPage.jsp">Add more questions</a> </body>
</html>
9. Create a servlet. AddQuestionServlet.java. and replace the existing code with the following code:
import ]ava.10.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import j ava.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.naming.Context;
import j avax.naming.InitialContext;
import j avax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import j avax.servlet.annotation.WebServlet;
import j avax.servlet.http.HttpServlet;
import j avax.servlet.http.HttpServletRequest;
import j avax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
SWebServlet(name = "AddQuestionServlet", urlPatterns : {"/AddQuestionServlet"})
public class AddQuestionServlet extends HttpServlet { DataSource ds;
Context context;
Connection con;
Statement st;
ResultSet rs;
PreparedStatement ps;
String insertQuery="Insert into AptitudeQuestions
(questionStem,optionl,option2,option3,option4,answer)values
/ O O O O O O \ •
/ /
String
qStem,optionl,option2,option3,option4,answer,correctCption; public AddQuestionServlet() {
try {
context = new InitialContext(); if (context == null) {
throw new RuntimeException("JNDI context
not found");
}
ds = (DataSource) context.lookup ("jdbc/OnlineTest");
if (ds == null) {
throw new RuntimeException("Datasource not
found");
}
con = ds.getConnection() ; st = con.createStatement(); ps=con.prepareStatement(insertQuery);
} catch (Exception e) {
System.out.println(e);
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8") PrintWriter out = response.getWriter(); try {
qStem=request.getParameter("qStem"); optionl=request. getParameter ("textcjptionl") ; option2=request.getParameter("textOption2"); option3=request.getParameter("textCption3"); option4=request.getParameter("textCption4"); correctCption=request.getParameter("options"); System.out.println(qStem+" "+optionl+" +option2+" "+option3+" "+option4+" "+correctOption); if(correctOption.equals("optionl")){
answer=request.getParameter("textCptionl");
}
else if(correctOption.equals("option2")){
answer=request.getParameter("textOption2");
}
else if(correctOption.equals("option3")){
answer=request.getParameter("textOption3");
}
else if(correctOption.equals("option4")){
answer=request.getParameter("textOption4");
}
else {
System.out.println("No answer is correct");
}
System.out.println(qStem+" "+optionl+" +option2+" "+option3+" "+option4+" "+answer);
ps.setstring(1, qStem); ps.setstring(2, optionl); ps.setstring(3, option2); ps.setstring(4, option3); ps.setstring(5, option4); ps.setstring(6, answer); int i=ps.executeUpdate();
System.out.println(i);
RequestDispatcher
rd=request.getRequestDispatcher("Success.j sp"); rd.forward(request, response); ps.close(); con.close(); context.close();
} catch (Exception e) {
System.out.println(e) ;
} finally {
out.close();
}
}
©Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { processRequest(request, response);
}
©Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { processRequest(request, response);
}
©Override
public String getServletlnfo() {
return "Short description";
}
}
10. Run the QuestionPage.jsp file.
No comments:
Post a Comment