如何从JSP页面调用servlet?

发布于 2024-10-05 21:40:04 字数 2399 浏览 3 评论 0 原文

可能的重复:
从 JSP 文件调用 servlet

我使用以下代码来调用 index.jsp 的 >conn.java(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>

但是,下面的代码不起作用。我想从 graphcon.jsp 调用 NewServlet

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随波逐流 2024-10-12 21:40:04

把你的问题说清楚。首先描述您想要做什么,然后描述您为完成您想要做的事情而遵循的步骤,然后描述您的步骤中出现的问题。我想您想要的是将您的请求重定向到一个 servlet。为此,请使用 sendredirect 函数。

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.

折戟 2024-10-12 21:40:04

如果您想从 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文