0

I was trying to make a simple E-Commerce website using MVC architecture in java. Initially I made a controller servlet which is named TestingServlet,a web.xml file,a bean class in the package com.bean under classes folder named DbBean and a Default.jsp,Header.jsp,Menu.jsp to start with. I compiled bean and the servlet classes. Then finally when I deployed the application on WebLogic server and run it, I got the following error-

the WebLogic log

As you can see in the log that it says it failed to compile JSP JSP/Menu.jsp which means the problem must be with Menu.jsp. Moreover, it says that in line 28 bean cannot be resolved. So, I checked that part of the code but it looks fine to me. What is causing the problem?

Following are all the files that I made- TestingServlet.java

import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import com.bean.DbBean;
public class TestingServlet extends HttpServlet
{
  public void init(ServletConfig conf)
   {
     System.out.println("Initiallizing ControllerServlet");
     ServletContext ctx = conf.getServletContext();

     ctx.setAttribute("base",conf.getInitParameter("base"));
     System.out.println("base = " +conf.getInitParameter("base"));
     ctx.setAttribute("imageUrl",conf.getInitParameter("imageUrl"));
     System.out.println("imageUrl = " +conf.getInitParameter("imageUrl"));


      DbBean bean = new DbBean();
      bean.setDbUrl(conf.getInitParameter("dbUrl"));
      bean.setDbUserName(conf.getInitParameter("userName"));
      bean.setDbPassword(conf.getInitParameter("password"));

      ctx.setAttribute("bean",bean);
      System.out.println("bean object successfully made,its properties set 
                           and set in app scope..");

      try
      {
        Class.forName(conf.getInitParameter("jdbcDriver"));
        System.out.println("Driver Class Loaded Successfully");
      }
      catch(Exception e)
          {
            System.out.println("Could not load the driver class");
          }
   }
   protected void doGet(HttpServletRequest req , HttpServletResponse res) 
                 throws ServletException,IOException
   {
     doPost(req,res);
     System.out.println("in doGet method");
   }
   protected void doPost(HttpServletRequest req , HttpServletResponse res) 
                  throws ServletException,IOException
   {  
     System.out.println("in doPost method");
      String base = "/jsp/";
     String url = base + "Default.jsp";
     String action = req.getParameter("action");

     if(action!=null)
     {
      if(action.equals("search"))
         url = base + "SearchResults.jsp";
       else if(action.equals("browseCatalog"))
               url = base + "BrowseCatalog.jsp";
       if(action.equals("productDetails"))
          url = base + "ProductDetails.jsp";
       if(action.equals("addShoppingItem") || 
          action.equals("updateShoppingItem") || 
          action.equals("deleteShoppingItem") || 
          action.equals("displayShoppingCart"))
        url = base + "ShoppingCart.jsp";
      if(action.equals("checkOut"))
        url = base + "CheckOut.jsp";
      if(action.equals("order"))
        url = base + "Order.jsp";
     }
     System.out.println("if part successfully executed");
     RequestDispatcher rd = req.getRequestDispatcher(url);
     System.out.println("RD object made successfully..");
     rd.forward(req,res);

     System.out.println("forward successfully executed..");
 }
}

DbBean.java

package com.bean;
import java.sql.*;
import java.util.Hashtable;

public class DbBean
  {
    public String dbUrl = "";
    public String dbUserName = "";
    public String dbPassword = "";

    public void setDbUrl(String url)
      {
        dbUrl = url;
      }
    public void setDbUserName(String userName)
      {
        dbUserName = userName;
      }
    public void setDbPassword(String password)
      {
         dbPassword = password;
      }

     public Hashtable getCategories()
      {
        Hashtable categories = new Hashtable();
        try
        {
          Connection conn = 
          DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
          Statement stmt = conn.createStatement();
          String sql = "select CategoryId,Category from Categories" +" ";
          ResultSet rs = stmt.executeQuery(sql);
          while(rs.next())
          {
            categories.put(rs.getString(1),rs.getString(2));
          }
          rs.close();
          stmt.close();
          conn.close();
        }
        catch(SQLException e)
        {}
        return categories;
      }

    } 

Default.jsp

<html>
<head>
 <title>Welcome</title>
</head>
<body>
<table>
 <tr>
    <td colspan="2">
        <jsp:include page="Header.jsp" flush = "true"/>
    </td>
 </tr>
 <tr>
   <td>
      <jsp:include page="Menu.jsp" flush="true"/>
   </td>
   <td valign="top">
      <H2>WELCOME TO MY E-MALL.</H2>
   </td>
 </tr>
</table>
</body>
</html>

Menu.jsp

<%@ page import="java.util.*" %>
<jsp : useBean id = "bean" scope = "application" class="com.bean.DbBean" />

<%
   String base = (String)application.getAttribute("base");
%>
<table cellspacing="0" cellpadding="5" width="150" border="0">
  <tr>
     <td bdcolor="F6F6F6">
        <font face="Verdana">Search</font>
        <form>
           <input type="hidden" name="action" value="search">
           <input type="text" name="keyword" size="10">
           <input type="submit" value="Go">
        </form>
     </td>
  </tr>
  <tr >
     <td bgcolor="F6F6F6">
       <font face="Verdana">
          Categories:
       </font>
     </td>
  </tr>
  <tr valign="top">
     <td bgcolor="F6F6F6">
        <%
          Hashtable categories = bean.getCategories();
          Enumeration categoryIds = categories.keys();
          while(categoryIds.hasMoreElements())
          {
             Object categoryId = categoryIds.nextElement();
             out.println("<a href=" +base +"?
                 action=browseCatalog&categoryId=" +categoryId.toString() 
                 +">" +categories.get(categoryId) +"</a><br>"); 
          }
        %>
     </td>
  </tr>
</table>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Gurdeep
  • 1
  • 2

1 Answers1

0

The jsp : useBean is for use with Objects that are Java Beans.

Is it the case that DbBean does not adhere to the JavaBeans conventions i.e. have a look at What is a JavaBean exactly? and maybe you could change DBean to adhere to the conventions or you could just drop the use of jsp : useBean and just instansiate and manipulate it in the scriptlet?

mkane
  • 880
  • 9
  • 16
  • Thanks for the reply but i want to know what exactly is the problem with this code? – Gurdeep Jan 11 '18 at 19:28
  • The problem is that the DbBean that you have here does not adhere to the convention i.e. All properties private (use getters/setters) A public no-argument constructor Implements Serializable. – mkane Jun 11 '18 at 12:20