0

Possible Duplicate:
Calling a servlet from JSP file

I have used following code to call a conn.java (servlet) from index.jsp. It works.

<%@page import= "java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import= "aa.conn" %>
<jsp:useBean id= "conne" class= "conn" scope= "session"/>
<jsp:setProperty name= "conne" property= "*"/>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP</title>

    </head>
    <body>
       <link rel="stylesheet" href="rowcolor.css" type="text/css">
      <%

      conne.con(request, response);
     ArrayList newlist=null;
    newlist=(ArrayList)request.getAttribute("data1");
    int noofrows=(Integer)newlist.get(0);
    int q = noofrows / 5;
    if(noofrows%5!=0)
        q+=1;
    out.println("Pages --->>>");
         for (int t = 1; t <= q; t++) {
            out.println("<a href=index.jsp?id=" + t + " name=" + t + "id=" + t + ">");
            out.println("  " + t);
            out.println("</a>");
        }
     conne.disp(request, response);
     conne.dispgraphtab(request, response);
      %>




    </body>
</html>

But, this following code doesn't work. I want to call NewServlet from graphcon.jsp.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import= "aa.NewServlet" %>
<jsp:useBean id= "co" class= "NewServlet" scope= "session"/>
<jsp:setProperty name= "co" property= "*"/>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

    </body>
</html>

What is the problem with this code? The error is:

exception
javax.servlet.ServletException: java.lang.InstantiationException: NewServlet
root cause 
java.lang.InstantiationException: NewServlet
Community
  • 1
  • 1
Parith
  • 85
  • 2
  • 6
  • 17
  • JSP Page – Parith Dec 01 '10 at 06:27
  • right now - the code examples you've used and the problem statement seem to be in complete disharmony. Can you spend some time to format your question properly, and make it somewhat clearer to understand. – anirvan Dec 01 '10 at 06:29
  • click on the "Edit" link to make the changes. putting your changes in the comments just adds to the mess! – anirvan Dec 01 '10 at 06:30
  • It gives the following exception javax.servlet.ServletException: java.lang.InstantiationException: NewServlet root cause java.lang.InstantiationException: NewServlet – Parith Dec 01 '10 at 07:17
  • There's a major misunderstanding here. In your examples you aren't using a servlet at all. In the first example you are just importing a Java class and writing raw Java code in JSP file instead of a Java class. In your second example you are attempting to instantiate a Java class as a bean. See the link of Bozho (and the links therein) to learn how to do it properly. – BalusC Dec 03 '10 at 11:51

2 Answers2

-1

Make your question clear. First describe what you want to do and then describe the steps you have followed to accomplish the things you want to do and then the problems occurring with your steps. I guess that what you want is want to redirect your request to one servlet. To do that use the sendredirect function.

Darshan Prajapati
  • 843
  • 2
  • 11
  • 33
  • 2
    i think it's more appropriate to put this as a comment and not as an 'answer' – anirvan Dec 01 '10 at 07:27
  • i hav already called conn.java from index.jsp using usebean tag. – Parith Dec 01 '10 at 08:38
  • but same coding for calling NewSrvlet.java from graphcon.jsp . . but it doesn't works. (Conn.java and NewServlet.java both r in same package. index.jsp and graphcon.jsp both in same package.) – Parith Dec 01 '10 at 08:41
  • Hello anirvan, I wanted to put it as comment only but option for adding comment was not there. So I replied as answer. – Darshan Prajapati Dec 03 '10 at 05:16
-1

If you want to call Servlet from the JSP file then do this:

In JSP File

<html>
    <head></head>
    <body>
        <form  action="/dataBase/DBInit" method="post">    // here call Servlet
            Design
        </form>
        <%       Logic         %>
    </body>
</html>

In Web.xml

<servlet>
    <servlet-name>dbname</servlet-name>
    <servlet-class>com.db.DBInit</servlet-class>     // Servlet Path Define Here
</servlet>
<servlet-mapping>
    <servlet-name>dbname</servlet-name>
    <url-pattern>/DBInit</url-pattern>
</servlet-mapping>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jimit Tank
  • 1,479
  • 5
  • 19
  • 25