Servlet2.java: 调用Servlet1.java中的Dopackage stest;import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*;public class Servlet2 extends HttpServlet { static final private String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>Servlet2</title></head>"); out.println("<body>"); Servlet1 s = new Servlet1(); out.println(s.Do()); out.println("<p>The servlet has received a GET. This is the reply.</p>"); out.println("</body></html>"); } //Clean up resources public void destroy() { } }Servlet1.java:package stest;import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*;public class Servlet1 extends HttpServlet { static final private String CONTENT_TYPE = "text/html; charset=GBK"; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head><title>Servlet1</title></head>"); out.println("<body>"); out.println("<p>The servlet has received a GET. This is the reply.</p>"); out.println("</body></html>"); } public int Do() { return 2; } //Clean up resources public void destroy() { } }
如果servlet里面有相应的初始化参数,
jsp也要相应的传过去
调用Servlet和Servlet的http请求响应
弄混了如果是Servlet的输出,当然不可能得到其对象,
但是直接调用就是类似一个普通的java类,想想吧,Servlet容器怎样对Servlet进行调用的!
调用Servlet1.java中的Dopackage stest;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class Servlet2 extends HttpServlet {
static final private String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet2</title></head>");
out.println("<body>");
Servlet1 s = new Servlet1();
out.println(s.Do());
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
}
//Clean up resources
public void destroy() {
}
}Servlet1.java:package stest;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body>");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
}
public int Do()
{
return 2;
}
//Clean up resources
public void destroy() {
}
}