HTML from 表單提交請求到servlet 例項
阿新 • • 發佈:2019-01-28
HTML原始碼展示:
<!DOCTYPE html> <html> <head> <style type="text/css"> div#container{width:100%;} div#menu {background-color:#e2e6ee;height:500px;width:50%;float:left;text-align:center; background-image:margin:0; color:#333333;} div#content {background-color:#EEEEEE;height:500px;width:50%;float:left;width:expression(window.screen.height);} body{background:#1d3647;} #from{ margin-top:20%;margin-left:10%;} #logo{margin-top:20%; float:right; margin-right:10%; } #adv{margin-top:27%; float:right; margin-right:1%; font:"宋體"; color:#777; font-size:14px;} </style> </head> <body> <br/> <br/> <div id="container"> <div id="menu"> <img src="platefrom/images/LOGO01.gif"id="logo"/><br/> <div id="adv"> <strong> 1- 地區商家資訊網門戶站建立的首選方案...</strong><br/><br/> <strong>2- 一站通式的整合方式,方便使用者使用...</strong><br/><br/> <strong>3- 強大的後臺系統,管理內容易如反掌...</strong> </div> </div> <div id="content"> <!--表單--> <form action="./LoginServlet" method="get" id="from"> <strong style="font:'宋體'; color:#777; font-size:16px;">登入後臺資訊管理</strong><br/><br/> 使用者名稱: <input type="text" name="username" /><br /><br/> 密 碼: <input type="password" name="password" /><br /> <br/><br/> <input type="submit" value="登 陸" style="font:'宋體'; font-size:16px;"/> <input type="submit" value="取 消" style="font:'宋體'; font-size:16px;"/> </form> </div> </div> </body> </html>
後臺Servlet
package com.platefrom.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //預設使用者名稱和密碼:admin和123456 String username=request.getParameter("username"); String password=request.getParameter("password"); //邏輯判斷操作 if(username.equals("admin")&&password.equals("123456")){ //功能介面 response.sendRedirect("./platefrom/main.html"); }else{ //登入頁面 response.sendRedirect("./platefrom/login.html"); } } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doGet(request, response); } }