Servlet实现我的第一个程序HelloWorld!

用myeclipse建立web项目,编写servlet。部署后,启动Tomcat服务器。

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {//继承HTTPServlet

	public void doGet(HttpServletRequest request, HttpServletResponse response)//重新定义doGet
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");//设置响应内容类型器
		PrintWriter out = response.getWriter();//取得响应输出对象
		String text = request.getParameter("text");//取得请求参数
		
		out.println("");
		out.println("  Hello Servlet");
		out.println("  ");
		out.println("

" + text + "!

"); out.println(" "); out.println(""); out.close(); } }

URL带上请求参数。

http://localhost:8080/FirstServlet/servlet/HelloServlet?text=HelloWorld

运行结果:



你可能感兴趣的:(Servlet,JSP)