gpt4 book ai didi

java - 如何在没有JSTL和EJB的情况下从servlet输出数据到jsp

转载 作者:行者123 更新时间:2023-12-01 13:02:11 25 4
gpt4 key购买 nike

我有一个任务要做:1. 用户输入登录名和密码。我为此做了以下jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div id="main">
<aside class="leftAside">
<h2>Authorization section</h2>
<p>Please enter your login and password and press "Go!"</p>
</aside>
<section>
<article>
<h1>Authorization</h1>
<div class="text">
<form method="GET" action="authorization">
<p>
<label for="login">Login</label>
<input type="text" name="login" id="login"/>
</p>
<p>
<label for="password">Password</label>
<input type="password" name="password" id="password"/>
</p>
<p>
<button type="submit">Go!</button>
</p>
</form>
</div>
</article>
</section>
</div>
  1. 如果可以,他们会取回访问历史记录,如果不行 - 再次登录页面,并带有一些授权失败的标记。我做了所有的连接和查询,但是如何管理它以及如何将所有数据输出到jsp?我不应该使用 beans 和 jSTL,而应该使用纯 servlet。

我的servlet在下面,我也不明白如何从用户那里获取密码和登录名以进一步发送它。

package Controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.*;


@WebServlet(name = "controller", loadOnStartup=1, urlPatterns = {"/"})
public class web_controller extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String userPath=request.getServletPath();
if ("/".equals(userPath)){
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
else {}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);

}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);

}
private static Connection getSQLConnection() {
Connection connection = null;
String dbUser = "root";
String dbPwd = "root";
String dbUrl = "jdbc:mysql://localhost:3306/test";
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
return connection;
}
catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

private static ResultSet history(Connection connection, String user) {
ResultSet result = null;
int userID = 0;
try{
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM users WHERE login=?");
preparedStatement.setString(1, user);
result = preparedStatement.executeQuery();
if (result.next()) {
userID = result.getInt("id");
}
result = null;
}
catch (SQLException e) {
e.printStackTrace();
}

try{
PreparedStatement history = connection.prepareStatement("SELECT * FROM history WHERE login=?");
getHistory.setInt(1, userID);
result = history.executeQuery();
return result;
}
catch (SQLException e) {
e.printStackTrace();
}

return result;
}
}

private static int USER_NOT_FOUND = 2;
private static int WRONG_PWD = 0;
private static int SUCCESS = 1;

private static int checkUser(Connection connection, String user, String passwd) {
ResultSet result = null;
String pwdMD5 = getMD5(passwd);
try{
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM users WHERE login=?");
preparedStatement.setString(1, user);
result = preparedStatement.executeQuery();
if (result.next()) {
String storedPwd = result.getString("passwd");
if (storedPwd.equals(pwdMD5)) {
return SUCCESS;
}
else return WRONG_PWD;
} else return USER_NOT_FOUND;
}
catch (SQLException e) {
e.printStackTrace();
}
return 0;
}

private static void addEntry(HttpServletRequest request, Connection connection, String user, String passwd, int success) {
int userID = 0;

try{
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM users WHERE login=?");
preparedStatement.setString(1, user);
ResultSet result = preparedStatement.executeQuery();
result.next();
userID = result.getInt("id");
}
catch (SQLException e) {
e.printStackTrace();
}
try{
PreparedStatement updHistory = connection.prepareStatement("INSERT INTO history (user_id, success, ipaddr) VALUES (?,?,?,?)");
updHistory.setInt(1, userID);
updHistory.setInt(2, success);
updHistory.setString(3, request.getRemoteAddr());
ResultSet result = updHistory.executeQuery();
result = updHistory.executeQuery();
}
catch (SQLException e) {
e.printStackTrace();
}

}

private static String getMD5(String pwd) {
String generatedMD5 = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(pwd.getBytes());
byte[] bytes = md.digest();
StringBuilder sbuilder = new StringBuilder();
for(int i=0; i< bytes.length ;i++)
{
sbuilder.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedMD5 = sbuilder.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return generatedMD5;

}
}

最佳答案

how to get passwd and login from the user to send it further?

尝试使用ServletRequest#getParameter()

示例代码

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);

String login = request.getParameter("login");
String password = request.getParameter("password");
}
<小时/>

出于安全考虑,切勿通过 GET 请求发送用户名/密码。

使用<form method="POST" action="authorization">

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);

String login = request.getParameter("login");
String password = request.getParameter("password");
}

有关更多示例,请查看 How to transfer data from JSP to servlet

<小时/>

--编辑--

how to pass the ResultSet from the servlet to the jsp?

只需使用 ServletRequest#setAttribute()ServletRequest#getAttribute()

请查看以下帖子:

关于java - 如何在没有JSTL和EJB的情况下从servlet输出数据到jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23441108/

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