servlet的doget和dopost示例

package myservlet;

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;

/**
 * Servlet implementation class MyServlet
 */
public class LoginServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
    /**
     * Default constructor.
     */
    public LoginServlet() {
        // TODO Auto-generated constructor stub
    }

 /**
  * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
     response.setContentType("text/html;charset=gb2312");
  response.setCharacterEncoding("gb2312");
  PrintWriter out=response.getWriter();
  out.println("<html>");
  out.println("<head>");
  out.println("<title>登录窗口</title>");
  out.println("</head>");
        out.println("<body>");
        out.println("<form id=\"form1\" name=\"form1\" method=\"post\" action=\"login\">");
        out.println("  <p align=\"center\">");
        out.println("    <label>用户");
        out.println("<input type=\"text\" name=\"username\" id=\"username\" />");
        out.println("</label>");
        out.println("</p>");
        out.println("<p align=\"center\">");
        out.println("<label>密码");
        out.println("<input type=\"text\" name=\"password\" id=\"password\" />");
        out.println("</label>");
        out.println("</p>");
        out.println("<p align=\"center\">");
        out.println("<label>");
        out.println("<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"提交\" /> ");
        out.println("</label><label><input type=\"reset\" name=\"reset\" id=\"reset\" value=\"重置\" />");
        out.println("</label>");
        out.println("</p>");
        out.println("</form>");
        out.println("</body>");
        out.println("</html>");
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  // TODO Auto-generated method stub
  response.setContentType("text/html;charset=gb2312");
  response.setCharacterEncoding("gb2312");
  request.setCharacterEncoding("gb2312");
  PrintWriter out=response.getWriter();
  out.println("<html>");
  out.println("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
  out.println("<html xmlns='http://www.w3.org/1999/xhtml'>");
  out.println("<HEAD><TITLE>用户信息</TITLE></HEAD>");
  out.println("<h1>你的信息如下:</h1>");
  out.println("<BODY>");
  out.println("<br>用户:"+request.getParameter("username")+"<br>");
  out.println("<br>密码:"+request.getParameter("password")+"<br>");  
  out.println("</BODY>");
  out.println("</html>");
 }

}

 

 

web.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<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_2_5.xsd"
   version="2.5">

  <display-name>用户登录实例</display-name>
  <description>
     用户登录实例
  </description>
  <servlet>
     <servlet-name>userlogin</servlet-name>
     <servlet-class>myservlet.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>userlogin</servlet-name>
     <url-pattern>/login</url-pattern>
  </servlet-mapping>

</web-app>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值