2010-12-01 101 views
0

可能重複:
Calling a servlet from JSP file如何從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 
+0

JSP頁 faree 2010-12-01 06:27:24

+0

現在 - 你已經使用的代碼示例和問題陳述似乎是完全不相適應的,你可以花一些時間來進行格式化。 – anirvan 2010-12-01 06:29:46

回答

-1

請清除您的問題。首先描述你想要做什麼,然後描述你完成你想要做的事情以及隨着步驟發生的問題所遵循的步驟。我想你想要的是將你的請求重定向到一個servlet。要做到這一點,使用sendredirect函數。

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