Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

BASE CODE: index.html <html> <head> <meta charset=\"UTF-8\"> <meta name=\"viewpo

ID: 3752279 • Letter: B

Question

BASE CODE:

index.html

<html>

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<h1>ToDo's: pick your options</h1>

<p> <a href="New Servlet" > Show ToDo List </a> </p>

<p> <a href="newjsp1.jsp" > Show ToDo List </a> </p>

</body>

</html>

newjsp.jsp (Showing list of items)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Products</h1>
        <table>
            <thead> <td> <b> Products </b></td></thead>
            <%
               String products[] = (String[]) session.getAttribute("products");
               if(products != null){
                for (int i = 0; i < products.length; i++)
                {
            %>
        <tr> <td>
                <% out.println(products[i]); %>
            </td> </tr>
        <%
            }

            }
        %>
    </table>
  
    <form action="newjsp1.jsp" method="POST">
                <input type="submit" value="Add Item">              
    </form>

</body>
</html>

newjsp1.jsp (Add items to list)

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Add a new item to ToDo list</h1>
        <form action="newjsp.jsp" method="POST">
                <label>New ToDo Item:</label>
                <input type="text" name="newItem">
                <input type="submit" value="Submit">              
       </form>
    </body>
</html>

NewServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    
      
        String products[] = {"Flipchart","Projector", "Whiteboard", "Chair"};
      
        HttpSession session = request.getSession(true);
      
        session.setAttribute("products", products);
      
        RequestDispatcher dispatcher = request.getRequestDispatcher("/newjsp.jsp");
      
        dispatcher.forward(request,response);
    }

}

With these files, the code needs to be edited so that when the user enters details to add items on the newjsp1.jsp page and clicks submit, the details appear on the newjsp.jsp page. Currently it only redirects without adding the details entered. Thanks

Explanation / Answer

<%@ page language="java" import="java.util.*,com.*,org.hibernate.*,org.hibernate.cfg.*" pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="&lt;%=basePath%&gt;">

  

<title>My JSP 'Delete.jsp' starting page</title>

  

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">   

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

  

<body>

<%! String id_1; int id; Session sess = null; %>

This is my JSP page. <br>

<a href="/OracleDemo/Display.jsp">Display</a>

<%

String id_1=request.getParameter("c1");

if(id_1!=null)

{

try

{

Configuration cf=new Configuration();

cf.configure();

SessionFactory fact = cf.buildSessionFactory();

sess = fact.openSession();

int count=0;

for(int i=0;i<id_1.length();i++)

{

id=Integer.parseInt(id_1);

String hql = "delete from DataIns where id = "+id;

Query query = sess.createQuery(hql);

int row = query.executeUpdate();

if(row>0)

count++;

}

if (count == 0)

{

out.println("Doesn't deleted any row!");

}

else

{

out.println("Deleted Row = " + count);

}

sess.beginTransaction();

sess.getTransaction().commit();

sess.close();

}

catch(Exception e)

{

System.out.println(e);

}

}

%>

</body>

</html>

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@page import="org.hibernate.Session"%>

<%@page import="org.hibernate.SessionFactory"%>

<%@page import="org.hibernate.id.Configurable"%>

<%@page import="org.hibernate.cfg.Configuration"%>

<%@page import="org.hibernate.Query"%>

<%@page import="com.DataIns"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="&lt;%=basePath%&gt;">

  

<title>My JSP 'index.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">   

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

  

<body>

<a href="/OracleDemo/Insert.jsp">Insert</a><br>

<%! int id,sem; String name; Session session1 = null; %>

<form action="">

<table border="2" width="220">

<tr><th>ID</th><th>NAME</th><th>SEM</th><th>UPDATE</th><th>DELETE</th></tr>

<%

SessionFactory cf= new Configuration().configure().buildSessionFactory();

session1=cf.openSession();

Query qur= session1.createQuery("from DataIns");

Iterator it=qur.iterate();

while(it.hasNext())

{

DataIns e=(DataIns)it.next();

id=e.getId();

name=e.getName();

sem=e.getSem();

%>

<tr>

<td><%=id%></td>

<td><%=name%></td>

<td><%=sem%></td>

<td><a href="/OracleDemo/Update.jsp?a=&lt;%=id%&gt;&amp;b=&lt;%=name%&gt;&amp;d=&lt;%=sem%&gt;">Edit</a></td>

<td><a href="/OracleDemo/Delete.jsp?c1=&lt;%=id%&gt;">Del</a> </td>

</tr>

<%

}

session1.close();

%>

</table>

  

</form>

</body>

</html>

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@page import="org.hibernate.Session"%>

<%@page import="org.hibernate.SessionFactory"%>

<%@page import="org.hibernate.id.Configurable"%>

<%@page import="org.hibernate.cfg.Configuration"%>

<%@page import="org.hibernate.Query"%>

<%@page import="com.DataIns"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="&lt;%=basePath%&gt;">

  

<title>My JSP 'Insert.jsp' starting page</title>

  

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">   

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

  

<body>

<%!

int id,sem;String name; Session session1 = null;

%>

This is my JSP page. <br>

<a href="/OracleDemo/index.jsp">Display</a><br>

<%

String num1=request.getParameter("t1");

if(num1 != null)

{

id=Integer.parseInt(num1);

name=request.getParameter("t2");

String sal=request.getParameter("t3");

sem=Integer.parseInt(sal);

try

{

Configuration cf=new Configuration();

cf.configure();

SessionFactory sessionFactory = cf.buildSessionFactory();

session1 =sessionFactory.openSession();

DataIns e=new DataIns(id,name,sem);

session1.beginTransaction();

session1.save(e);

session1.beginTransaction().commit();

session1.flush();

session1.close();

out.println("<h1>Data Inserted Successfully</h1>");

}

catch(Exception e)

{

System.out.println("e="+e.getMessage());

}

}

%>

<form >

<table width="352" border="1">

  

<tr>

<th>Id</th>

<td><input name="t1" type="text"></td>

</tr>

<tr>

<th> Name </th>

<td><input name="t2" type="text"></td>

</tr>

<tr>

<th>Sem </th>

<td><input name="t3" type="text"></td>

</tr>

  

<tr>

<th colspan="2"><input type="submit"value="Submit" >

</th>

</tr>

</table>

</form>

</body>

</html>

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%@page import="org.hibernate.cfg.Configuration"%>

<%@page import="org.hibernate.SessionFactory"%>

<%@page import="org.hibernate.classic.Session"%>

<%@page import="org.hibernate.Transaction"%>

<%@page import="com.DataIns"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="&lt;%=basePath%&gt;">

  

<title>My JSP 'Update.jsp' starting page</title>

  

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">   

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

  

<body>

<%

String i=request.getParameter("a");

String name=request.getParameter("b");

String s=request.getParameter("d");

String sub=request.getParameter("s1");

int id=0;

int sem=0;

if(sub != null)

{

id=Integer.parseInt(i);

try

{

sem=Integer.parseInt(s);

Configuration cf=new Configuration();

cf.configure();

SessionFactory fact = cf.buildSessionFactory();

Session sess = fact.openSession();

Transaction tr = sess.beginTransaction();

DataIns e = (DataIns)sess.get(DataIns.class,id);

e.setName(name);

e.setSem(sem);

tr.commit();

sess.update(e);

sess.close();

out.println("<h1>Updated successfully!</h1>");

out.println("<a href="/OracleDemo/index.jsp">Display</a><br>");

}

catch(Exception e)

{

System.out.println("e="+e.getMessage());

}

}

%>

  

This is my JSP page. <br>

<form name="f1" >

<table width="371" border="1">

<tr>

<th> Id </th>

<td><input name="a" type="text" value="<%= i %>" >

</td>

</tr>

<tr>

<th> Name </th>

<td><input name="b" type="text" value="<%= name %>" ></td>

</tr>

<tr>

<th> Sem </th>

<td><input name="d" type="text" value="<%= s %>" ></td>

</tr>

  

<tr>

<th colspan="2"><input type="submit" name="s1" value="Save" >

</th>

</tr>

</table>

</form>

</body>

</html>