使用httpServlet方法开发
使用继承HttpServlet方法开发
显示HeeloWorld以及当前日期
默认Doget方式提交
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wangzhi.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest res, HttpServletResponse resp) throws ServletException, java.io.IOException { resp.getWriter().println("I'm a student"); } protected void doPost(HttpServletRequest res, HttpServletResponse resp) throws ServletException, java.io.IOException { resp.getWriter().println("I'm a senior student"); } }
相比较post而言安全性低
怎么使用Dopost方式?
login.html
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<html> <body> <form action="/web/MyServlet" method="post"> <input type="String" name="username" value="username"/> <input type="submit" value="login"/> </form> </body> </html>
MyServlet.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.wangzhi.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { protected void doGet(HttpServletRequest res,HttpServletResponse resp) throws ServletException, java.io.IOException { resp.getWriter().println("this doGet"); } protected void doPost(HttpServletRequest res,HttpServletResponse resp) throws ServletException, java.io.IOException { resp.getWriter().println("This is doPost"+res.getParameter("username")); } }
用eclipse 跑起来第一个servlet程序,
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.myservlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class MyServlet1 */ @WebServlet("/MyServlet1") public class MyServlet1 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public MyServlet1() { 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().println("hello,world!"+new java.util.Date().toString()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 合二为一 this.doGet(request,response); } }