Servlet(三)HttpServlet
以HttpServlet创建Servlet
1、在FirstServlet同目录下建立HServlet.java
package com.hunhun; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; public class HServlet extends HttpServlet{ <span style="white-space:pre"> </span>protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ resp.getWriter().println("doGet"+req.getParameter("username")); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ resp.getWriter().println("doPost"+req.getParameter("username")); } }
2、web.xml
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>com.hunhun.FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/FirstServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>GServlet</servlet-name> <servlet-class>com.hunhun.GServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>GServlet</servlet-name> <url-pattern>/GServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>HServlet</servlet-name> <servlet-class>com.hunhun.HServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>HServlet</servlet-name> <url-pattern>/HServlet</url-pattern> </servlet-mapping> </web-app>
3、编译文件,reload myweb项目,注意每次重新修改*Servlet.java文件,都需要重新编译,并重新加载 myweb项目或重启tomcat7
4、浏览器访问 http://localhost:8080/myweb/HServlet?username=hunhun,可看到
doGethunhun5、在myweb目录下建立login.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action="/myweb/HServlet" method="post"> 用户名:<input type="text" name="username"> <input type="submit" value="登录"> </form> </body> </html>6.浏览器访问http://localhost:8080/myweb/login.html,填写用户名登陆则可看到
doPosthunhun
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。