1,105,254 Community Members

Edit Data Using JSP / Servlet

Member Avatar
josiefrut
Newbie Poster
13 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi guys,

I need your help. I was trying to update a certain data in my database using jsp and servlet. However i can't figure out why it wouldn't update the data. :(

I'd greatly appreciate it if you could check my codes below. THANKS MUCH!

Here's my jsp

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


        <title></title>
    </head>
    <body>
        <c:import url="../menu.jsp"/> <br>
        <jsp:useBean id="foo" class="com.dole.cdms.application.Application" scope="request"/>
        <form action="ApplicationServlet" method="POST">
            <table class="form">
                <tr><td>Application ID:</td><td>  <input type="text" name="appId" value="<jsp:getProperty name="foo" property="appId"/>"></td></tr>
                <tr><td>Application Name:</td><td>  <input type="text" name="appName" value="<jsp:getProperty name="foo" property="appName"/>"></td></tr>
                <tr><td>Application Status:</td><td> <input name="appStatus" type="radio" value="1" checked="checked"> Active
                        <input name="appStatus" type="radio" value="0">Inactive</td></tr>
                <tr><td></td><td><input type="Submit" value="Update"></td></tr>
            </table>
        </form>
    </body>
</html>

Here's my bean class:

import java.sql.Date;

/**
 *
 * @author Administrator
 */
public class Application {

    private String appId = null;
    private String appName = null;
    private int appStatus = 0;
    private int sysId = 0;
    private String sysUser = null;
    private String sysHost = null;
    private Date sysDateTime = null;



    public Application(){
    }

    public Application (String app_id,
                        String app_name,
                        int app_status,
                        int sys_id,
                        String sys_user,
                        String sys_host,
                        Date sys_datetime){

        this.appId=app_id;
        this.appName=app_name;
        this.appStatus=app_status;
        this.sysDateTime=sys_datetime;
        this.sysHost=sys_host;
        this.sysId=sys_id;
        this.sysUser=sys_user;
        
    }

    /**
     * @return the appId
     */
    public String getAppId() {
        return appId;
    }

    /**
     * @param appId the appId to set
     */
    public void setAppId(String appId) {
        this.appId = appId;
    }

    /**
     * @return the appName
     */
    public String getAppName() {
        return appName;
    }

    /**
     * @param appName the appName to set
     */
    public void setAppName(String appName) {
        this.appName = appName;
    }

    /**
     * @return the appStatus
     */
    public int getAppStatus() {
        return appStatus;
    }

    /**
     * @param appStatus the appStatus to set
     */
    public void setAppStatus(int appStatus) {
        this.appStatus = appStatus;
    }

    /**
     * @return the sysId
     */
    public int getSysId() {
        return sysId;
    }

    /**
     * @param sysId the sysId to set
     */
    public void setSysId(int sysId) {
        this.sysId = sysId;
    }

    /**
     * @return the sysUser
     */
    public String getSysUser() {
        return sysUser;
    }

    /**
     * @param sysUser the sysUser to set
     */
    public void setSysUser(String sysUser) {
        this.sysUser = sysUser;
    }

    /**
     * @return the sysHost
     */
    public String getSysHost() {
        return sysHost;
    }

    /**
     * @param sysHost the sysHost to set
     */
    public void setSysHost(String sysHost) {
        this.sysHost = sysHost;
    }

    /**
     * @return the sysDateTime
     */
    public Date getSysDateTime() {
        return sysDateTime;
    }

    /**
     * @param sysDateTime the sysDateTime to set
     */
    public void setSysDateTime(Date sysDateTime) {
        this.sysDateTime = sysDateTime;
    }

}

For the Model Class:

public class ApplicationModel {
   Application editApplication(String id, String name) throws ClassNotFoundException, SQLException, NamingException {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;

        try {

            conn = ConnectionFactory.getConnection();
            statement = conn.createStatement();
            PreparedStatement ps = null;
            ps = conn.prepareStatement("UPDATE tblApplication SET appID=?, appName = ? WHERE appID = ?");
            ps.setString(1, id);
            ps.setString(2, name);
            ps.setString(3, id);
            ps.executeUpdate();
//            int i = statement.executeUpdate("UPDATE tblApplication SET appID='" + id + "', appName='" + name + "' WHERE appID='" + id + "'");
//            System.out.println(name);
//            if (i != 0) {
//                System.out.println("Application updated!");
//            } else {
//                System.out.println("Error 0.o");
//            }


            return null;
        } finally {
            if (statement != null) {
                statement.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
    }

Lastly for the Servlet:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException, NamingException {
        response.setContentType("text/html;charset=UTF-8");

        String mode = null;

        if (mode== null){
            mode=request.getParameter("editApp");
            if(mode!=null) {
                editApplication(request, response);
            }
        }
        } else {
            response.sendRedirect("/CNRDocSys/error/error.jsp");
        }


       protected void editApplication(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException, NamingException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String id = request.getParameter("appId");
            String name=request.getParameter("appName");
            ApplicationModel model = new ApplicationModel();

            Application application = model.editApplication(id,name);
            request.setAttribute("foo", application);

            getAllApplications(request, response);

        } catch (Exception e) {
            response.sendRedirect("/CNRDocSys/error/error.jsp");

        } finally {
            out.close();
        }
    }

Hope you can help me with this, guys. Thanks much, really. ^_^ God bless!

Member Avatar
josiefrut
Newbie Poster
13 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

By the way, all I'm getting is a blank page.. and the log is giving me this error:

Feb 28, 2011 4:12:30 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet ApplicationServlet threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)
at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:126)
at com.dole.cdms.application.ApplicationServlet.processRequest(ApplicationServlet.java:79)
at com.dole.cdms.application.ApplicationServlet.doGet(ApplicationServlet.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:619)
Feb 28, 2011 4:12:37 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet ApplicationServlet threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:435)
at javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:126)
at com.dole.cdms.application.ApplicationServlet.processRequest(ApplicationServlet.java:79)
at com.dole.cdms.application.ApplicationServlet.doGet(ApplicationServlet.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:859)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1555)
at java.lang.Thread.run(Thread.java:619)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: