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>
?