一个简单的用户登录界面

今天我们学习了如何实现一个简单的用户登陆界面,具体要用到两个jsp和一个servlet,具体操作如下:

首先创建一个用户登录界面,inputview.jsp,其源代码如下所示:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<form name="form1" method=post action="Login">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="pwd"><br>
<br>
<input type="submit" value="登录">
</form>
</body>
</html>

好了,一个用户登陆界面就简单的做好了,接下来还要实现登陆界面进入到登陆成功的界面successview.jsp,其源代码如下所示:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎</title>
</head>
<body>
您是合法用户,欢迎进入本空间!
</body>
</html>

这些做好以后,就要创建一个servlet来进行页面的跳转,我们使用的是doPost方法,具体操作如下:

package com.cqvie;


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


/**
 * Servlet implementation class Login
 */
public class Login extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Login() {
        super();
        // TODO Auto-generated constructor stub
    }


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}


/**
* @param pwd 
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response, Object pwd) throws ServletException, IOException {

String username = request.getParameter("username");
String password = request.getParameter("pwd");

if(username.equals("admin")&& pwd.equals("123")){


response.sendRedirect("successview.html");
}
else
response.sendRedirect("inputview.html");

}


}

好了,一个简单的用户登录界面就做好了。

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页