·初识Servletimportjava.io.*;导入所importjavax.servlet.*需的包import javax.servlet.http.*:extendsHttpServletpublic class HelloServlet继承HttpServlet类public void doGet(HttpServletRequest request,5处理请HttpServletResponseresponse)2求的方throwsServletException,IOException法response.setContentType("text/html;charset=gb2312");PrintWriterout=response.getWriterO;out.println("<html>");将数据out.println("<head><title>Servlet</title></head>");发送给out.println("<body>");客户端out.println("你好,欢迎来到Servlet世界");out.println("</body>");out.println("</html>");Servlet输出HTML标签和内容out.close();
• 初识Servlet import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gb2312"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println(" <head><title>Servlet</title></head>"); out.println(" <body>"); out.println("你好,欢迎来到Servlet世界"); out.println(" </body>"); out.println("</html>"); out.close(); } } 继承HttpServlet类 Servlet输出HTML标签和内容 导入所 需的包 处理请 求的方 法 将数据 发送给 客户端
Servlet与JSP关系Servlet与JSP之间的关系一JSP在服务器上转译的文件MyJspjsp.javaMyJsp.jsp(.jsp转译后的.java文件)<%@pageout.write("<html>lrln");contentType="text/html;charset=gbk"%>out.write("<head>lrln");JSP转译后的.java文件与<html>out.write(""Servlet的处理方式一样<title>MyJsp.jsp</title>lrln");<head><title>MyJsp</title>out.write("</head>lrln");</head>out.write("<body>lrln");<body>out.write("ThisismyJSP page.Servlet是JSP技术的基础Thisis my<br>lrln");</body>out.write("</body>lrln");</html>out.write("</html>/rln");
Servlet与JSP关系 • Servlet与JSP之间的关系 – JSP在服务器上转译的文件 out.write("<html>\r\n"); out.write(" <head>\r\n"); out.write(" <title>MyJsp.jsp</title>\r\n"); out.write(" </head>\r\n"); out.write(" <body>\r\n"); out.write(" This is my JSP page. <br>\r\n"); out.write(" </body>\r\n"); out.write("</html>\r\n"); <%@ page contentType="text/html;charset=gbk" %> <html> <head> <title>MyJsp</title> </head> <body> This is my JSP page. <br> </body> </html> MyJsp.jsp MyJsp_jsp.java (.jsp转译后的.java文件) JSP转译后的.java文件与 Servlet的处理方式一样 Servlet是JSP技术的基础
如何使用Servlet前台页面调用一FORM表单的编写·后台Servlet的创建一使用Eclipse的Servlet向导创建Servlet代码模板一根据表单提交方法,确定接收方法一接收请求数据、处理数据、返回响应页面
如何使用Servlet • 前台页面调用 – FORM表单的编写 • 后台Servlet的创建 – 使用Eclipse的Servlet向导创建Servlet代码模板 – 根据表单提交方法,确定接收方法 – 接收请求数据、处理数据、返回响应页面
FORM表单的method属性FORM表单提交的方法- Get和Post<%@pagecontentType="text/html;charset=gbk"%><html><head><title>MyJsp</title></head>FORM的方法有哪些?<body>name="form1"><formaction="helloservlet'method="?<!-表单内容--></form>Get | PostGet、Post提交数据与</body>Get与Post有什么区别?Servlet有什么关系吗?</html>
FORM表单的method属性 • FORM表单提交的方法 – Get和Post <html> <head> <title>MyJsp</title> </head> <body> This is my JSP page. <br> </body> </html> <%@ page contentType="text/html;charset=gbk" %> <html> <head> <title>MyJsp</title> </head> <body> <form action="helloservlet" method=" ? " name="form1“> <!- 表单内容 -> </form> </body> </html> FORM的方法有哪些? Get | Post Get 与 Post有什么区别? Get、Post提交数据与 Servlet有什么关系吗?
Servlet编程模式Servlet编程基本模式一处理Get和Post请求importjava.io.lOException;importjavax.servlet.*importjavax.servlet.http.*;publicclassHelloServletextendsHttpServletpublicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,lOExceptionlldoGet方法体内容使用Get方法提交,触发doGet()方法publicvoiddoPost(HttpServletRequestrequest,HttpServletResponsethrowsServletException.IOExceptionresponse)lldoPost方法体内容使用Post方法提交,触发doPost(方法
Servlet编程模式 • Servlet编程基本模式 – 处理Get和Post请求 import java.io.IOException; import javax.servlet.*; import javax.servlet.http.*; public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //doGet方法体内容 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //doPost方法体内容 } } 使用Get方法提交,触发doGet()方法 使用Post方法提交,触发doPost()方法