如何使用Java将参数从JSP发布到HTTP

时间:2016-01-09 23:47:05

标签: java jsp http servlets post

我想将参数从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;

    }

}

1 个答案:

答案 0 :(得分:-1)

更改

  @WebServlet("/admin/*") 

@WebServlet("/admin") 

你可以使用:

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

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