3 回答
TA贡献1860条经验 获得超9个赞
jsp要调用servlet中自定义的方法需要把自定的方法放在doPost或者doGet或者init方法中。
举例如下:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(req, resp);
System.out.println("doGet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
//调用自定义方法
returnBack(req,resp);
System.out.println("doPost");
}
//这里是自定义方法
public void returnBack(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
System.out.println("really!go home");
}
}
- 3 回答
- 0 关注
- 3121 浏览
添加回答
举报