gpt4 book ai didi

java - 如何使用 Java 将参数从 JSP 传递到 HTTP

转载 作者:行者123 更新时间:2023-12-01 06:11:00 24 4
gpt4 key购买 nike

我想将参数从 JSP 发送到 HTTP Servlet。但它不起作用:(

我想创建一个按钮来发送信息以禁用/启用用户。

我对 JSP 和 HTTP 还很陌生。

希望有人能帮助我。

我希望这足以概述

这是我的代码:

admin.jsp

<%@page import="model.*"%>
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
List<Category> categories = (List<Category>) request.getAttribute("categories");
List<User> users = (List<User>) request.getAttribute("users");
User credentials = (User) request.getSession().getAttribute("credentials");
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<jsp:include page="header.jsp"></jsp:include>
<jsp:include page="/WEB-INF/navbar.jsp"></jsp:include>
<h1 style="color:green;">Admin Control Page</h1>

<h2>categories</h2>
<% for (Category category : categories) { %>
<a href="<%=request.getContextPath()%>/search?category=<%=category.getName()%>"><%=category.getName()%></a><br/>
<% } %>
<form method="post" action="<%=request.getContextPath()%>/admin" accept-charset="iso-8859-1">
<br/>
add new category: <input name="categoryName" />
<input type="hidden" name="event" value="createCategory" />
<input type="submit" />
</form>

<h2>users</h2>
<% for (User user : users){
if(user.getRole().getId()!= 1){
out.println("<a href='"+request.getContextPath()+"/user/"+user.getUsername()+"'><b>"+user.getUsername()+"</b></a>");
int id = user.getId();
%>

<%if(user.isEnabled()){ %>

//My Problem is here

<form action="/admin" method="POST">
<input value="<%user.getId();%>">
<input type="submit" value="Submit" />
</form>



<%}else if(!user.isEnabled()){
%>
// TODO Button

<%}%>
<p><hr>
<%
}
}
%>
</body>
</html>
<小时/>

AdminController.java

package controller;

import java.io.IOException;

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 dao.DatabaseManager;
import model.Category;
import model.User;

@WebServlet("/admin/*")
public class AdminController extends HttpServlet {

private DatabaseManager db = DatabaseManager.getInstance();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

// check admin, if not "error"
User user = (User) req.getSession().getAttribute("credentials");
if (user == null || !user.getRole().getName().equals("admin")) {
resp.sendError(403);
return;
}

// load page
req.setAttribute("categories", db.getCategoryDAO().findAll());
req.setAttribute("users", db.getUserDAO().findAll());
req.getRequestDispatcher("/WEB-INF/admin.jsp").forward(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

// event
String event = req.getParameter("event");

// create new category
if (event.equals("createCategory")) {
String categoryName = req.getParameter("categoryName").trim();

try {
Category cat = new Category();
cat.setName(categoryName);
db.getCategoryDAO().create(cat);
} catch (IllegalArgumentException e) {
// error
req.setAttribute("msg", e.getMessage());
doGet(req, resp);
return;
}

// Create successful


resp.sendRedirect(req.getContextPath() + "/admin");
return;
}

String idTemp = req.getParameter("id");

try{
int id = Integer.parseInt(idTemp);
User user = db.getUserDAO().findById(id);
user.setEnabled(false);

} catch(IllegalArgumentException e){
e.getMessage();
return;
}
resp.sendRedirect(req.getContextPath() + "/admin");
return;

}

}

最佳答案

改变

  @WebServlet("/admin/*") 

@WebServlet("/admin") 

您可以使用:

 action="<%=request.getContextPath()%>/admin" or action="admin".

如果您仍然遇到错误,请提及错误名称:)

关于java - 如何使用 Java 将参数从 JSP 传递到 HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34700323/

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