时间:2023-05-30 23:45:01 | 来源:网站运营
时间:2023-05-30 23:45:01 来源:网站运营
2018-5-25 JSP_Web_MVC开发(18)-Servlet简介及例子:JSP+DAO的设计模式开发之中,使用最多的概念:request、session(属性、注销)、application。此类开发存在以下问题:public abstract class HttpServlet extends GenericServlet implements java.io.Serializable
HttpServlet是一个抽象类,抽象类的使用原则就是必须有子类,而且子类要覆写方法。此时选择要覆写的方法为doGet():protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
例 定义HelloServlet类import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@SuppressWarnings("serial")public class HelloServlet extends HttpServlet { @Override public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter out=resp.getWriter();//取得客户端输出 out.println("<html>"); out.println("<head><title>第一个Servlet</title></head>"); out.println("<body>"); out.println("<h1><font color=/"red/">Hello World!</font></h1>"); out.println("</body>"); out.println("</html>"); out.close(); }}
Servlet程序并不是依靠主方法运行的,它需要Web容器执行,而Web容器要想识别此程序,则需要修改web.xml文件,增加Servlet的配置。 <!--此处表示定义一个Servlet的配置--> <servlet> <!--配置的Servlet名称,与servlet-mapping对应--> <servlet-name>helloServlet</servlet-name> <!--配置此Servlet所在路径,一定要保存在Web-INF/classes下--> <servlet-class>cn.mytest.servlet.HelloServlet</servlet-class> </servlet> <!--Servlet访问的映射路径--> <servlet-mapping> <!--与处理的Servlet对应--> <servlet-name>helloServlet</servlet-name> <!--访问路径,一定要有“/”--> <url-pattern>/hello.do</url-pattern> </servlet-mapping>
如果要进行乱码处理,处理的是服务器端回应的乱码程序,此时可以加入如下代码:public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); resp.setContentType("text/html"); PrintWriter out=resp.getWriter();//取得客户端输出 out.println("<html>"); out.println("<head><title>第一个Servlet</title></head>"); out.println("<body>"); out.println("<h1><font color=/"black/">这是Hello World!</font></h1>"); out.println("</body>"); out.println("</html>"); out.close();}
今后通过Servlet输出数据,一定要控制编码问题。而且代码必须在resp.getWriter()之前!package cn.mytest.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebInitParam;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class HelloServlet */@WebServlet( urlPatterns = { "/hello.do" }, initParams = { @WebInitParam(name = "helloServlet", value = "cn.mytest.servlet.HelloServlet") })public class HelloServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public HelloServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); }}
注意:web.xml没有任何改动。关键词:例子