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>用戶(hù)");
??????? 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>用戶(hù)信息</TITLE></HEAD>");
??out.println("<h1>你的信息如下:</h1>");
??out.println("<BODY>");
??out.println("<br>用戶(hù):"+request.getParameter("username")+"<br>");
??out.println("<br>密碼:"+request.getParameter("password")+"<br>");??
??out.println("</BODY>");
??out.println("</html>");
?}
}
web.xml內(nèi)容如下:
<?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>用戶(hù)登錄實(shí)例</display-name>
? <description>
???? 用戶(hù)登錄實(shí)例
? </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>
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
