0 Replies Latest reply on Apr 6, 2006 6:58 AM by Susitha Bandara

    Update a database through a portlet

    Susitha Bandara Newbie

      Hi!

      I am using JBServer-4.0.2 and JBPortal-2.0. I am going to update a table in a Oracle database. I am calling a jsp page to a portlet and thruogh that jsp file I am going to call a servlet to update the database.

      NewStudentDetailsServlet.java

      package classes;
      
      import java.io.*;
      import javax.servlet.*;
      import javax.servlet.http.*;
      import java.sql.*;
      
      public class NewStudentDetailsServlet extends HttpServlet {
      
       Connection con=null;
       PreparedStatement stmt=null;
      
       public void init(ServletConfig config) throws ServletException {
      
      
      
       }
      
       public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
       {
      
       PrintWriter out=res.getWriter();
       res.setContentType("text/html");
      
      
       try {
       Class.forName("oracle.jdbc.driver.OracleDriver");
       con=DriverManager.getConnection("jdbc:oracle:thin:@cmbrnd5:1521:sedna","ip_jpf","ip_jpf");
       stmt=con.prepareStatement("INSERT INTO PERA VALUES(?,?,?)");
      
       stmt.clearParameters();
       stmt.setString(1,req.getParameter("name"));
       stmt.setString(2,req.getParameter("address"));
       stmt.setString(3,req.getParameter("regno"));
       stmt.executeUpdate();
      
       out.println("Thank you!");
      
       }
      
       catch(Exception e){
       e.printStackTrace();
       }
      
       }
      
       public void destroy()
       {
      
       try {
       if(con!=null)
       con.close();
       }
      
       catch(Exception e) {}
      
       }
      
      }
      


      NewStudentPortlet.java


      package classes.newstudent;
      
      import javax.portlet.ActionRequest;
      import javax.portlet.ActionResponse;
      import javax.portlet.GenericPortlet;
      import javax.portlet.PortletException;
      import javax.portlet.PortletSecurityException;
      import javax.portlet.RenderRequest;
      import javax.portlet.RenderResponse;
      import javax.portlet.UnavailableException;
      import javax.portlet.*;
      import java.io.IOException;
      
      public class NewStudentPortlet extends GenericPortlet
      {
       private static final String JSP_PATH = "/WEB-INF/jsp";
      
       public void init()
       {
       }
      
       protected void doView(RenderRequest req, RenderResponse res) throws PortletException, IOException, UnavailableException
       {
       res.setContentType("text/html");
       PortletRequestDispatcher prd = this.getPortletContext().getRequestDispatcher(JSP_PATH + "/index.jsp");
       prd.include(req, res);
       }
      
       public void processAction(ActionRequest aRequest, ActionResponse aResponse) throws PortletException, PortletSecurityException, IOException
       {}
      
       public void render(RenderRequest req, RenderResponse res) throws PortletException, IOException
       {
       res.setTitle(getTitle(req));
       doDispatch(req, res);
       }
      }



      index.jsp

      <html>
      <body bgcolor="#FFDDCC">
      <form name="New" method="GET" action="/servlet/NewStudentDetailsServlet">
      <align="center">
      <BR>
      <table>
      <tr>
      <TH><B><h2>Enter your details</h2></b></TH>
      </TR><TR>
      <td><b>NAME</b></td>
      <td><input type=text name="name"></td>
      </tr><tr>
      <td><b>ADDRESS</b></td>
      <td><TEXTAREA COLS=30 ROWS=4 NAME="address"></textarea></td>
      </tr><tr>
      <td><b>REG.NO</b></td>
      <TD><input type=text name="regno"></TD>
      </tr><tr>
      <TD><input type=submit value="SUBMIT"></TD>
      <TD><input type=reset value="CANCEL"></TD>
      </TR>
      </TABLE>
      </align>
      </form>
      </body>
      </html>


      web.xml

      <?xml version="1.0"?>
      <!DOCTYPE web-app
       PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
       "http://java.sun.com/dtd/web-app_2_3.dtd">
      <web-app>
       <servlet>
       <servlet-name>NewStudentDetailsServlet</servlet-name>
       <servlet-class>NewStudentDetailsServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
       <url-pattern>/servlet/NewStudentDetailsServlet</url-pattern>
       <servlet-name>NewStudentDetailsServlet</servlet-name>
      </servlet-mapping>
       </web-app>


      The jsp is loading correctly to the portlet. But after I click submit it gives 404- page not found. Can somebody tell me what is the error...

      Thanx...