servlet的doget和dopost示例

系统 1949 0

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>");
 }

}

 
servlet的doget和dopost示例
 
servlet的doget和dopost示例

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>

 

servlet的doget和dopost示例


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论