您的位置:首页 > Web前端 > HTML

利用servlet和html写一个简单的登录

2015-03-13 20:28 507 查看
写一个简单的html登录页面,然后利用servlet来进行验证账号密码。

html代码:

<!DOCTYPE html>

<html>

<title>登陆页面</title>

<head>

<meta charset="UTF-8">

<title>Insert title here</title>

</head>

<body>

<form action = "LogOnYZ" method = "post">

用户名:<input type = "text" name = "name"/>

<br>密码:<input type = "password" name = "password"/>

<br><input type = "submit" value = "登录">

</form>

</body>

</html>





这里使用doPost方法,为了安全起见。

servlet代码:





package work3;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.Servlet;

import javax.servlet.ServletConfig;

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 LogOnYZ

*/

@WebServlet("/LogOnYZ")

public class LogOnYZ extends HttpServlet {

private String User;

private String Password;



/**

* @see HttpServlet#HttpServlet()

*/

public LogOnYZ() {

super();

// TODO Auto-generated constructor stub

}

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

response.setContentType("text/html; charset = UTF-8");

PrintWriter out = response.getWriter();

User = request.getParameter("name");

Password = request.getParameter("password");

if(User.equals("abc") && Password.equals("123")){

out.print("<h1>登陆成功</h1>");

out.print("欢迎 : " + User);

}else{

out.print("<h1>账号或密码错误!</h1>");

}

out.close();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: