首页 > 代码库 > Servlet(三)HttpServlet
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=http://www.mamicode.com/"登录">>6.浏览器访问http://localhost:8080/myweb/login.html,填写用户名登陆则可看到doPosthunhun
Servlet(三)HttpServlet
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。