如何从JSP页面调用servlet?

时间:2010-12-01 06:20:14

标签: java jsp servlets usebean

  

可能重复:
  Calling a servlet from JSP file

我使用以下代码从conn.java调用index.jsp(servlet)。它有效。

<%@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>

但是,以下代码不起作用。我想从NewServlet致电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>

此代码有什么问题?错误是:

exception
javax.servlet.ServletException: java.lang.InstantiationException: NewServlet
root cause 
java.lang.InstantiationException: NewServlet

2 个答案:

答案 0 :(得分:-1)

明确问题。首先描述您想要做什么,然后描述您完成要执行的操作所遵循的步骤,然后描述步骤中出现的问题。我想你想要的是将你的请求重定向到一个servlet。为此,请使用sendredirect函数。

答案 1 :(得分:-1)

如果要从JSP文件调用Servlet,请执行以下操作:

在JSP文件中

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

在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>
相关问题