JSP中application(return String)用法详例

1.JSP代码:

<%--
    Document   : Application
    Created on : 2009-10-4, 13:18:53
    Author     : lucifer
--%>


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        get Server Info:
        <%=application.getServerInfo()%>
        <br>
        get Context Path:
        <%=application.getContextPath()%>
        <br>
        get Real Path:
        <%=application.getRealPath("/Application.jsp")%>
        <br>
    </body>
</html>


2.输出:

get Server Info: Apache Tomcat/6.0.18
get Context Path: /TestMethod
get Real Path: D:/Program Files/NetBeans 6.7.1/www/TestMethod/build/web/Application.jsp

以下是一个基本的用户注册示,使用JSP中的application和session: register.jsp: ``` <html> <head> <title>User Registration</title> </head> <body> <h1>User Registration</h1> <% String errorMessage = (String) request.getAttribute("errorMessage"); if (errorMessage != null) { out.println("<p style=\"color:red\">" + errorMessage + "</p>"); } %> <form method="post" action="register.do"> <label for="username">Username:</label> <input type="text" id="username" name="username"><br> <label for="password">Password:</label> <input type="password" id="password" name="password"><br> <input type="submit" value="Register"> </form> </body> </html> ``` register.do: ``` <%@ page import="java.util.*" %> <%@ page import="javax.servlet.*" %> <%@ page import="javax.servlet.http.*" %> <% String username = request.getParameter("username"); String password = request.getParameter("password"); // Validate input if (username == null || username.trim().length() == 0 || password == null || password.trim().length() == 0) { request.setAttribute("errorMessage", "Please enter a username and password"); request.getRequestDispatcher("register.jsp").forward(request, response); return; } // Check if username already exists ServletContext application = getServletContext(); Map<String, String> users = (Map<String, String>) application.getAttribute("users"); if (users == null) { users = new HashMap<String, String>(); application.setAttribute("users", users); } if (users.containsKey(username)) { request.setAttribute("errorMessage", "Username already exists"); request.getRequestDispatcher("register.jsp").forward(request, response); return; } // Add user to application scope users.put(username, password); // Add user to session scope HttpSession session = request.getSession(); session.setAttribute("username", username); response.sendRedirect("welcome.jsp"); %> ``` welcome.jsp: ``` <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome <%= session.getAttribute("username") %></h1> <p>You have successfully registered.</p> <p><a href="logout.do">Logout</a></p> </body> </html> ``` logout.do: ``` <%@ page import="javax.servlet.*" %> <%@ page import="javax.servlet.http.*" %> <% // Invalidate session HttpSession session = request.getSession(false); if (session != null) { session.invalidate(); } // Redirect to login page response.sendRedirect("login.jsp"); %> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值