How to call a servlet from JSP page?

Parith picture Parith · Dec 1, 2010 · Viewed 9.6k times · Source

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

Answer

Darshan Prajapati picture Darshan Prajapati · Dec 1, 2010

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.