JSP中的登录表单 [英] Login form in JSP

查看:234
本文介绍了JSP中的登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处输入代码Hiya,到目前为止,我根本没有使用过JSP,我发现一些在线教程很难从中学习,我需要编写简单的登录表单..对于我来说,这不是问题PHP,但是在JSP中却是一个大问题.这就是我将如何在PHP中使用简单明了的HTML做到这一点:

<form action="action.php" method="post">
<input name="username" type="text" />
<input name="password" type="password" />
</form>

这是action.php

<?php
$user = $_POST['username'];
$pwd = $_POST['password'];
if($user != "" && $pwd != "")
{
  ... check whether md5 value of $pwd for username $user matches the value in database..
if so then redirect somewhere..
}
?>

我如何在JSP中做类似的事情

解决方案

与PHP不同,您通常使用 JSP 进行演示只要.尽管事实上您可以使用脚本(在其中包含原始Java代码的<% %>东西)在JSP文件中编写Java代码,但您应该(直接或间接)使用 JSTL ,其中JSTL的核心"是最重要的.您可以使用 EL (表达语言)来访问在页面,请求,会话和应用程序范围内可用的数据.

首先,创建一个 JSP 文件,该文件基本上包含以下内容:

<form action="${pageContext.request.contextPath}/login" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit">
</form>

然后创建一个 servlet 类,该类具有如下实现的doPost():

String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userDAO.find(username, password);

if (user != null) {
    request.getSession().setAttribute("user", user); // Logged in!
    response.sendRedirect(request.getContextPath() + "/home"); // Redirect to some home page.
} else {
    request.setAttribute("message", "Unknown login, try again."); // Print it in JSP as ${message}.
    request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Redisplay same JSP with error message.
}

最后对servlet类进行注释,如下所示:

@WebServlet("/login")

这意味着您可以通过 http://example.com/webapp/login 调用它就像在<form action>中所做的一样.

另请参见:

祝你好运.

enter code hereHiya, so far I didn't use JSP at all and I find some online tutorials to be very complicated to learn from them, I need to make simple login form .. its not problem for me to do it in PHP but in JSP ist a big problem. Here is how I'd do it in PHP plain and simple, HTML :

<form action="action.php" method="post">
<input name="username" type="text" />
<input name="password" type="password" />
</form>

Here is action.php

<?php
$user = $_POST['username'];
$pwd = $_POST['password'];
if($user != "" && $pwd != "")
{
  ... check whether md5 value of $pwd for username $user matches the value in database..
if so then redirect somewhere..
}
?>

How would I do something like this in JSP

解决方案

Unlike PHP, you normally use JSP for presentation only. Despite the fact you can write Java code in a JSP file using scriptlets (the <% %> things with raw Java code inside), you should be using (either directly or indirectly) a servlet class to control requests and execute business logic. You can use taglibs in JSP to generate/control the output. A standard taglib is JSTL, with the JSTL 'core' being the most important. You can use EL (Expression Language) to access data which is available in page, request, session and application scopes.

To start, create a JSP file which contains basically the following:

<form action="${pageContext.request.contextPath}/login" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit">
</form>

Then create a servlet class which has the doPost() as below implemented:

String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userDAO.find(username, password);

if (user != null) {
    request.getSession().setAttribute("user", user); // Logged in!
    response.sendRedirect(request.getContextPath() + "/home"); // Redirect to some home page.
} else {
    request.setAttribute("message", "Unknown login, try again."); // Print it in JSP as ${message}.
    request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Redisplay same JSP with error message.
}

Finally annotate the servlet class as below:

@WebServlet("/login")

Which means that you can invoke it by http://example.com/webapp/login as done in <form action>.

See also:

Good luck.

这篇关于JSP中的登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆