您的位置:首页 > 编程语言 > Java开发

eclipse servlet简单的用户登录

2017-07-02 11:59 337 查看
servlet程序

package test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class first_servlet
*/
@WebServlet("/first_servlet")
public class first_servlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public first_servlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String name=request.getParameter("inputname");
String pwd=request.getParameter("inputpwd");
if(name!=null&&name.equals("martin")) {
response.sendRedirect("http://blog.csdn.net/martind");
}else {
response.sendRedirect("https://www.baidu.com");
}

}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}

}


登录界面
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<title>my first bootstrap</title>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet"
href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<link rel="stylesheet"
href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
crossorigin="anonymous">
<script
src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
</head>

<body>
<div class="container">
<h1 class="page-header  col-md-offset-4">用户登录</h1>
<form action="first" method="post" class="form-horizontal">

<div class="form-group">
<label for="inputname" class="col-md-4 control-label">用户名
 <span class="glyphicon glyphicon-user"></span>
</label>
<div class="col-md-4">
<input type="text" class="form-control" name="inputname"
placeholder="请输入用户名" />
</div>
</div>
<div class="form-group">
<label for="inputpwd" class="col-md-4 control-label">密码
 <span class="glyphicon glyphicon-lock"></span>
</label>
<div class="col-md-4">
<input type="password" class="form-control" name="inputpwd"
placeholder="请输入密码" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-5">
<button class="btn btn-primary">注册</button>
<button class="btn btn-primary">登录</button>
</div>
</div>

</form>
</div>
</body>

</html>


xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>first_servlet</servlet-name>
<servlet-class>test.first_servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>first_servlet</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>
</web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: