如何将servlet连接到另一个servlet

时间:2014-03-02 11:11:00

标签: java jsp servlets

我创建了两个servlet,我通过单独运行它来获取输出。但是,当我将两个servlet组合在一起时,我遇到了错误。

我有servlet1servlet2

这是我编写的用于从servlet2

调用servlet1的代码
request.getRequestDispatcher("/servlet2.java").forward(request, response);

这是在jsp页面中显示servlet2代码的代码

request.getRequestDispatcher("/result.jsp").forward(request, response);

但对我来说,servlet1并没有为此过程提供servlet2

servlet1.java

try
{
    String name=request.getParameter("name");  
    String carname=request.getParameter("carname");
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:userdetail","","");
    Statement st=con.createStatement();
    st.executeUpdate("insert into userdetail values('"+name+"','"+carname+"')"); 
    out.println(name);
    out.println(carname);
    request.getRequestDispatcher("/servlet2").forward(request, response);
}

servlet2

try
{
    String tata1=request.getParameter("cname");
    String tata=request.getParameter("agency");
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection con=DriverManager.getConnection("jdbc:odbc:wsdsn","","");
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("select agency from webservice where carname='"+tata1+"'");
    request.setAttribute("agency1",agency1);
    request.getRequestDispatcher("/result.jsp").forward(request, response);
}

result.jsp中

input type="text" name="agency" value='<%=request.getAttribute("agency1")%>'  />

0 个答案:

没有答案