使用servlet和JSP创建样本登录页面? [英] Create a sample login page using servlet and JSP?

查看:75
本文介绍了使用servlet和JSP创建样本登录页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个示例登录页面,用于验证用户名和密码.如果用户提供了正确的凭据,则该页面将导航到其他页面,否则将消息从servlet返回到同一登录页面.

I developed a sample login page for validating username and password. If the user gives correct credentials the page will navigate to some other page, else return a message from the servlet to the same login page.

我在这里附上了样品code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>`
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org   /TR/html4/loose.dtd">`
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Insert title here</title>`
    </head>
    <body>
        <form action="Login" method="post">
            <input type="text" name="Name"/><br>
            <input type="password" name=Pass><br><br><br>
            <input type="submit" value="submit"/><br>
            <%if(request.getAttribute("message")!=(""))
            out.println(request.getAttribute("message"));
            else
            out.println("");%>
            <h1>Hi This is JSP sample code </h1>
            <font color="blue" size="25">
            <marquee>Please login to Work with JSP</marquee></font>
            <%java.text.DateFormat df = new java.text.SimpleDateFormat("MM/dd/yyyy"); %>
            <h1>Current Date: <%= df.format(new java.util.Date()) %> </h1>
        </form>
    </body>
</html>

servlet代码,Login.java:

And servlet code, Login.java:

package com.controller;
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 Login extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public Login() {
        super();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("Name");
        String password = request.getParameter("Pass");
        response.setContentType("text/html");
        if(username.equals("sugan") && password.equals("raj")) {
            response.getWriter().println("<html><body><Marquee>Welcome to JSP!!!</marquee></body></html>");                
        } 
        else {                
            String message = "OOps!!! Invalid Username/Password";
            request.setAttribute("message", message);
            request.getRequestDispatcher("/FirstJSP.jsp").forward(request, response);                
        }
    }
}

它工作正常,但是在加载登录页面时,它会自动显示Null.然后,如果用户提供了错误的凭据,它将显示实际消息.如何在运行时禁用Null消息?

It works fine, but while loading the login page it automatically displays Null. Then if the user gives the wrong credentials, it displays the actual message. How do I disable the Null message during run time?

推荐答案

您正在使用==将消息与空字符串进行比较.

You're comparing the message with the empty string using ==.

首先,您的比较是错误的,因为该消息将为null(而不是空字符串).

First, your comparison is wrong because the message will be null (and not the empty string).

第二,这是错误的,因为必须将对象与equals()而不是与==进行比较.

Second, it's wrong because Objects must be compared with equals() and not with ==.

第三,这是错误的,因为您应该避免在JSP中使用scriptlet,而应使用JSP EL,JSTL和其他自定义标签:

Third, it's wrong because you should avoid scriptlets in JSP, and use the JSP EL, the JSTL, and other custom tags instead:

<c:id test="${!empty message}">
    <c:out value="${message}"/>
</c:if>

这篇关于使用servlet和JSP创建样本登录页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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