gpt4 book ai didi

mysql - 我的问题是如何从一个jsp页面定向到另一个jsp页面?

转载 作者:行者123 更新时间:2023-11-29 18:47:42 25 4
gpt4 key购买 nike

     This is my login.jsp page

<%@ page import ="java.sql.*" %>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<!DOCTYPE html>
<html>
<style>
/* Full-width input fields */
input[type=text], input[type=password] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}

/* Set a style for all buttons */
button {
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}

/* Extra styles for the cancel button */
.cancelbtn {
padding: 14px 20px;
background-color: #f44336;
}

/* Float cancel and signup buttons and add an equal width */
.cancelbtn,.signupbtn {
float: left;
width: 50%;
}

/* Add padding to container elements */
.container {
padding: 16px;
}

/* Clear floats */
.clearfix::after {
content: "";
clear: both;
display: table;
}

/* Change styles for cancel button and signup button on extra small screens */
@media screen and (max-width: 300px) {
.cancelbtn, .signupbtn {
width: 100%;
}
}
</style>
<head>
<meta charset="ISO-8859-1">
<title>Registration Form</title>
</head>
<form method="post" action="success.jsp">

<div class="container">
<h2>Login Form</h2>
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="Username" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="Password" required>
<div class="clearfix">
<button type="submit" value="submit">Login</button>

</div>
</div>
</html>
<%
String Username= request.getParameter("Username");
String Password = request.getParameter("Password");
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","root");
Statement st = con.createStatement();
ResultSet rs;
rs=st.executeQuery("SELECT * FROM customers WHERE Username='" + Username + "' and Password='" + Password + "'");
if (rs.next()) {
session.setAttribute("Username",Username);
// out.println("Welcome " + Username);
//out.println("<a href='logout.jsp'>Log out</a>");
response.sendRedirect("success.jsp");
}
else
{
out.println("Invalid Username or password Please try again");
}
%>
I am using eclipse,tomcat,mysql db.When i run login.jsp page it is asking me validation details and total code works fine but in output page i am getting
O/P:Welcome null.It is throwing me null value.And also it is directly going to else loop and giving me output in the output page like this "Invalid Username or Password Please try again".I used both HTML and java code in jsp file is that correct???

success.jsp:

<%
if ((session.getAttribute("Username") == "null") || (session.getAttribute("Password") =="null")) {
%>
You have not logged in<br/>
<a href="login.jsp">Please Login</a>
<%} else {
%>
Welcome <%=session.getAttribute("Username")%>
<a href='logout.jsp'>Log out</a>
<%
}
%>

欢迎<%=session.getAttribute("用户名")%>
//预期输出Welcome rohan542 login.jsp 直接从 login.jsp 到 success.jsp。换句话说,它必须从登录页面直接到成功页面。没有堆栈跟踪,我正在获取输出,但不是我所期望的。我认为代码有一点错误,请纠正它。我在java中使用 session 。代码必须检查用户名和密码是否正确,如果正确,则必须验证并转到成功页面,除此之外我还提供了一个注销链接直接注销

最佳答案

你可以使用

<c:redirect url = "your-url"/>

用于重定向到较新网址的标记。

由于您的代码位于同一页面中,因此您的操作不应指向 success.jsp 页面。它应该指向相同的登录页面。请修改问题描述以避免混淆。

请更改以下行。

 <form method="post" action="success.jsp"> to
<form method="post" action="login.jsp">

关于mysql - 我的问题是如何从一个jsp页面定向到另一个jsp页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44513105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com