Home Answers Viewqa JSP-Servlet error in web application

 
 


megha singhal
error in web application
3 Answer(s)      a year and 2 months ago
Posted in : JSP-Servlet

In your application when i am trying to execute it from the below page

<%@ page language="java" import="java.util.*;"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>Servlet Application</TITLE>

  <script language="javascript">
function editRecord(id){

    window.location.href="UserServlet/"+id; 
  }


  function deleteRecord(id){

    window.location.href="deleteUser/"+id; 
  }
</script>
 </HEAD>

 <BODY>
 <br>
<table align="center">

</table>
<br>
  <table width="600px"  align="center" style="background-color:#EDF6EA;border:1px solid #000000;">
 <tr><td colspan=9 align="center" height="10px"></td></tr>

    <tr><td colspan=9 align="center"><a href="addUser.jsp" style="font-weight:bold;color:#cc0000;">Add New User</a></td></tr>
<tr><td colspan=9 align="center" height="10px"></td></tr>
  <tr style="background-color:#7BA88B;font-weight:bold;">
     <td>User Id</td><td>UserName</td><td>First Name</td>
     <td>Last Name</td><td>City</td><td>Sate</td>
     <td>Country</td><td>Edit</td><td>Delete</td>
  </tr>
    <%
    String bgcolor="";
    int count=0;
    List viewList = new ArrayList();
    Iterator  viewItr;

    if(request.getAttribute("userList")!=null && request.getAttribute("userList")!="")
    {
        List userList =  (ArrayList)request.getAttribute("userList");
        Iterator itr = userList.iterator();

        while(itr.hasNext())
        {

            if(count%2==0)
            {
             bgcolor = "#C8E2D1";
            }
            else
            {

                bgcolor = "#EAF8EF";
            }

            viewList = (ArrayList)itr.next();
            int id = Integer.parseInt(viewList.get(0).toString());
            viewItr = viewList.iterator();
            %>
            <tr style="background-color:<%=bgcolor%>;">
            <%  
            while(viewItr.hasNext())
            {

                %>
                <td><%=viewItr.next()%></td>

                <%

            }
            count++;
            %>
            <td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=id%>);" ></td>
                <td><input type="button" name="delete" style="background-color:#ff0000;font-weight:bold;;color:#ffffff;" value="Delete" onclick="deleteRecord(<%=id%>);"></td>
            </tr>
            <%
        }
    }
    if(count==0)
    {
        %>
        <tr><td colspan="9" align="center">&nbsp;</td></tr>
            <tr><td colspan="9" align="center">No Record Avaliable</td></tr>
        <%
    }
    %>
     <tr><td colspan=9 align="center" height="2px"></td></tr>
  </table>  
 </BODY>
</HTML>

when i click on edit button it is going on follwoing servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.sql.*;
import java.util.*;

public class UserServlet extends HttpServlet{ 

   public void doGet(HttpServletRequest request, HttpServletResponse response)
                                   throws ServletException,IOException{
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            String str[] = request.getRequestURI().toString().split("/");

         int id = Integer.parseInt(str[3]);
            System.out.println("MySQL Connect Example.");
            Connection conn = null;
            String url = "jdbc:mysql://localhost:3306/";
            String dbName = "user_register";
            String driver = "com.mysql.jdbc.Driver";
            String userName = "root"; 
            String password = "root";

        //  out.println(request.getRequestURI());
            Statement st;
            try {
                Class.forName(driver).newInstance();
                conn = DriverManager.getConnection(url+dbName,userName,password);
                System.out.println("Connected to the database");


                ArrayList userList=null;
                String query = "select * from userregister where id="+id;
                System.out.println("query " + query);
                st = conn.createStatement();
                ResultSet  rs = st.executeQuery(query);


                while(rs.next())
                {

                  userList =new ArrayList();
                  userList.add(rs.getInt(1));
                  userList.add(rs.getString(2));
                  userList.add(rs.getString(4));
                  userList.add(rs.getString(5));
                  userList.add(rs.getString(6));
                  userList.add(rs.getString(7));
                  userList.add(rs.getString(8));

                }

                request.setAttribute("userList",userList);

                String nextJSP = "/editUser.jsp";
                RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
                dispatcher.forward(request,response);
                conn.close();
                System.out.println("Disconnected from database");
            } catch (Exception e) {
            e.printStackTrace();
            }
  }
}

but not redicting to editUser.jsp that is following

<%@ page language="java" import="java.util.*;"%>
<html>
<head>
</head>
<body>
<%!
int id;
String first_name="";
String last_name="";
String username="";
String city="";
String state="";
String country="";
List  userList=null;
%>

<%

if(request.getAttribute("userList")!=null && request.getAttribute("userList")!="")
{
        userList = (ArrayList)request.getAttribute("userList");
        id=Integer.parseInt(userList.get(0).toString());
        username=userList.get(1).toString();
        first_name=userList.get(2).toString();
        last_name=userList.get(3).toString();
        city=userList.get(4).toString();
        state=userList.get(5).toString();
        country=userList.get(6).toString();
        //out.println(id);
}
%>

<form name="userform" method="post" action="../editUser">
<br><br><br>
<table align="center" width="300px" style="background-color:#EDF6EA;border:1px solid #000000;">
<input type="hidden" name="id" value="<%=id%>">
<tr><td colspan=2 style="font-weight:bold;" align="center">Edit User</td></tr>
<tr><td colspan=2 align="center" height="10px"></td></tr>
    <tr>
        <td>First Name</td>
        <td><input type="text" name="first_name" value="<%=first_name%>"></td>
    </tr>
    <tr>
        <td>Last Name</td>
        <td><input type="text" name="last_name" value="<%=last_name%>"></td>
    </tr>
    <tr>
        <td>UserName</td>
        <td><input type="text" name="username" value="<%=username%>"></td>
    </tr>
    <tr>
        <td>Password</td>
        <td><input type="password" name="password" value=""></td>
    </tr>

    <tr>
        <td>City</td>
        <td><input type="text"  name="city" value="<%=city%>"></td>
    </tr>
    <tr>
        <td>State</td>
        <td><input type="text" name="state" value="<%=state%>"></td>
    </tr>
    <tr>
        <td>Country</td>
        <td><input type="text" name="country" value="<%=country%>"></td>
    </tr>

    <tr>
        <td></td>
        <td><input type="submit" name="Submit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td>
    </tr>
    <tr><td colspan=2 align="center" height="10px"></td></tr>
</table>
</form>
</body>
</html>

please help me

View Answers

March 28, 2012 at 11:43 AM


We are providing you another application. Check it.

1)application.jsp:

<%@ page import="java.sql.*" %>
<html>
<head>
<script language="javascript">
function editRecord(id){
    var f=document.form;
    f.method="post";
    f.action='edit.jsp?id='+id;
    f.submit();
}
function deleteRecord(id){
    var f=document.form;
    f.method="post";
    f.action='delete.jsp?id='+id;
    f.submit();
}
</script>
</head>
<body>

<br><br>
<form method="post" name="form">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr>
<%
Connection con = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "test";
String driver = "com.mysql.jdbc.Driver";
String userName ="root";
String password="root";

int sumcount=0;
Statement st;
try{
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url+db,userName,password);
String query = "select * from employee";
st = con.createStatement();
ResultSet rs = st.executeQuery(query);
%>

<%
while(rs.next()){
%>
<tr><td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=rs.getString(1)%>);" ></td>
<td><input type="button" name="delete" value="Delete" style="background-color:#ff0000;font-weight:bold;color:#ffffff;" onclick="deleteRecord(<%=rs.getString(1)%>);" ></td>
</tr>
<%
}
%>
<%
}
catch(Exception e){
e.printStackTrace();
}
%>
</table>
</form>
</body>
</html>

2)edit.jsp:

<%@page language="java"%>
<%@page import="java.sql.*"%>
<form method="post" action="update.jsp">
<table border="1">
<tr><th>Name</th><th>Address</th><th>Contact No</th><th>Email</th></tr>
<%
String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
String query = "select * from employee where id='"+no+"'";
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while(rs.next()){
%>
<tr>
<td><input type="text" name="name" value="<%=rs.getString("name")%>"></td>
<td><input type="text" name="address" value="<%=rs.getString("address")%>"></td>
<td><input type="text" name="contact" value="<%=rs.getInt("contactNo")%>"></td>
<td><input type="text" name="email" value="<%=rs.getString("email")%>"></td>
<td><input type="hidden" name="id" value="<%=rs.getString(1)%>"></td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Update" style="background-color:#49743D;font-weight:bold;color:#ffffff;"></td>
</tr>
<%
}
}
catch(Exception e){}
%>
</table>
</form>

March 28, 2012 at 11:44 AM


continue..

3)update.jsp:

<%@page import="java.sql.*"%>

<%
String ide=request.getParameter("id");
int num=Integer.parseInt(ide);
String name=request.getParameter("name");
String address=request.getParameter("address");
int contact=Integer.parseInt(request.getParameter("contact"));
String email=request.getParameter("email");
try{
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root", "root");
Statement st=null;
st=conn.createStatement();
st.executeUpdate("update employee set name='"+name+"',address='"+address+"',contactNo="+contact+",email='"+email+"' where id='"+num+"'");
response.sendRedirect("/examples/jsp/application.jsp");
}
catch(Exception e){
System.out.println(e);
    }
%>

4)delete.jsp:

<%@page import="java.sql.*"%>
<%

String id=request.getParameter("id");
int no=Integer.parseInt(id);
int sumcount=0;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = conn.createStatement();
st.executeUpdate("DELETE FROM employee WHERE id = '"+no+"'");
response.sendRedirect("application.jsp");
}
catch(Exception e){}
%>

March 28, 2012 at 12:33 PM


thanx for your help but i need in servlet form as there is lots of other functionality which i have to embed in it..

can you help me with this..

just tell me the way i can extract data from the database in jsp form field but logic i have to apply to apply in servlets only for extraction of database fields and in jsp just i have to print the values extracted through servlet...









Related Pages:
error in web application
error in web application  In your application when i am trying to execute it from the below page <%@ page language="java" import="java.util....; We are providing you another application. Check it. 1)application.jsp
web application
web application  what is lazy loading in web based application
web application
web application  Dear friend, could u plz tell me wats difference bt web server,application server,container with thanks praveen
ISAPI Filter Issue Error from Web Service ("Error:- InternetOpenUrl()")
ISAPI Filter Issue Error from Web Service  <p>I have writen MFC application for ISAPI filter which will authenticate and authorization... from web service.Error is "Error:- InternetOpenUrl().". This error is coming
Web application
Web application  in web applications which file will execute first either servlet or jsp
web application
web application  Develop a web application to print back the inputs from the user(name, date of birth, address). The solution should have a JSP file which accepts the userâ??s name and sent to Servlets which puts the name
confused about an error in my web application deploying to Tomcat - Java Server Faces Questions
confused about an error in my web application deploying to Tomcat  Could any one please test this application and tell me what's the problem... org.apache.catalina.core.ApplicationContext log INFO: HTMLManager: start: Starting web application
servlet-error
servlet-error  where we declare a servlet errors in web application
web application to internet - JavaMail
web application to internet  how to connect my web application to internet
error
error  When I deploye the example I have this message cannot Deploy HelloWorld Deployment Error for module: HelloWorld: Error occurred during...; lineNumber: 8; columnNumber: 20; Deployment descriptor file WEB-INF/web.xml in archive
web application - WebSevices
web application  how to create a j2ee web application using java abstract class & interface
connecting to timesten in web application
connecting to timesten in web application  Please give me the steps connecting to timesten in web application. Am using jsp
Web application security - Security
Web application security  Hi, Expert how to hide url in webpage or fix the url in every web page in java.Please give any suggestion or idea
I had this error while deploying a web services in jboss
I had this error while deploying a web services in jboss  Error configuring application listener of class...="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi
HTTP Status 404 - /web/login/showLogin.action Error in Struts 2 - Struts
HTTP Status 404 - /web/login/showLogin.action Error in Struts 2  Hi... application which is explained in the Roseindia website but I am using the differnet.../all the jars required for struts and all other thing web(web project starts from
Directory structure of a web application
Directory structure of a web application   Explain the directory structure of a web application.    The directory structure of a web application consists of two parts. A private directory called WEB-INF. A public
Web application - Spring
Web application  I need one simple complete web application example using springs with database connection,i saw ur demo but it is not succfient.can u send to raghavendra@oneapps.com.I am a beginner
application cntext file error
application cntext file error  i got an error when execute the file in junit using application context file org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in file
java web application - Ant
tell me the steps to build and deploy a small web application on MyEclipse?(like... a directory structure for a Web application. a)Create a WEB-INF directory... deployment descriptor in the WEB-INF directory for the Web application. 5
log4j in web application
log4j in web application  Hi all, I am new to log4j. I want to use log4j in my web application. I need to write the debug,info,warn into a file... and how to get the logger object in web application. Thanks suresh
Java web Application
Java web Application  Hi! I have developed a java web application project using netbean IDE with glassfish server. My question is how to create a runnable file from this. How can we make it to a single file? I think it is war
Quartz in web-application
Quartz in web-application  As I am new to Quartz i don't know how to use it in web-application.I am able to run it in eclipse as a stand alone application i had tried out some example but am not able to succeed any help
Web
Web  Application is not working in any versions of IE. Its working good in firefox. Please help me.. Thanks in advance
Flex web application
Flex web application  Hi.... How can i develop web based applications in Flex? please give me the whole procedure so i can make web apps in flex..... Thanks
Web application - business layer
Web application - business layer   Design your business layer to be stateless, which helps to reduce resource contention and increase... with a stateless Web application business layer. If you perform business
what is the difference between distributed application and web application?
what is the difference between distributed application and web application?  what is the difference between distributed application and web application
Web Application Common Architecture
Web Application Common Architecture  Hi, Any one tell me, while project constructing , how they architect it which means using some layers and specify importance of layers. And also best way to follow program flow
servlet question on web application
servlet question on web application   how to write a programg on the web application. create two modules like admin and customer. customer can registrate with user id and password. after finishing of his registration he'll get
draw chart in web application
draw chart in web application  how to draw bar chat from the record store in database? i.e. draw the bar chart according to selected record
Web application - JSP-Servlet
Web application   Helo can you please help me in knowing how to use jsp/servlets which allows users to post question in a forum so that other users can also view the question and respond to it.For instance your application here
web application security - Security
web application security  Hello, Experts how can i prevent that a copy url cannot run in same brower or different browser again .Please Suggest any idea i already check if a session is null then forwward a login page
Websphere application error - Hibernate
Websphere application error  i have received this following error [10/5/10 13:19:40:592 EDT] 00000043 SystemOut O java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl incompatible
Error java.lang.NullPointerException hibernate - Hibernate
Error java.lang.NullPointerException hibernate  hello everybody; I'm developping a web application using Struts 1.1 , Hibernate 3.1 and Mysql , and I've got this message error: Etat HTTP 500
php mysql web application tutorial
php mysql web application tutorial  How to create a simple php mysql web application? Please post an example or tutorial
ISAPI Filter Error (
ISAPI Filter Error (  MFC: For ISAPI Filter geting error from Web service i.e ("Error:- InternetOpenUrl()") I have writen MFC application for ISAPI... but when we are doing load testing we are getting error from web service.Error
HTS Web Application Framework
HTS Web Application Framework       The HTS Web Application Framework is a PHP and Javascript based framework designed to make simple web applications easy to design and implement
Web technologies - JSP-Servlet
Web technologies  Hello, I am a new web developer, i have deployed a web application on tomcat server, i started the server, when i am trying to call the application through Internet explorer i am getting the following error
Web application? - Java Interview Questions
Web application?  What is webapplication?  Hi friend, Any application that uses Web Technologies including web browsers, web servers and Internet protocols is called Web Application. A web application can
To publish jsp web application in intranet
To publish jsp web application in intranet  I am making web application in jsp with netbean. I want to publish this web application in LAN/Intranet.So by publishing web application it can be access through LAN/Intranet
ssl comunication between a desktop application and a web application
ssl comunication between a desktop application and a web application  Hi Lusiano, I am trying to implement a ssl comunication between a desktop application and a web application. can you please share code with me. Please
Deployment Error - Struts
Deployment Error  When I try to deploy application ?struts-examples-1.3.8.war? file in Tomcat 5.5 Web Server it is working fine. Whereas when I deploy the same web application in Weblogic8.1 Application Server, I am getting
Why Struts in web Application - Struts
Why Struts in web Application  Hi Friends, why struts introduced in to web application. Plz dont send any links . Need main reason for implementing struts. Thanks Prakash
creating a friendly url for web application
creating a friendly url for web application  Hi all, I don't know how to create a friendly url for a web application. Please help me to resolve this problem. Thanks, Suresh