来自控制器的错误响应 JSP

Wrong response from controller JSP

我正在尝试使用 Controller 和模型做一个 JSP 教程,它是一个简单的表单,可以接收一些数据、进行一次计算和 returns 一个文本。该教程在这个 youtube 视频中: enter link description here 但是我尝试在 Eclipse 中的 Netbeans 中做同样的事情。当我发送日期时,我得到这个:

但我希望通过表格获得数据的响应。这是我的控制器代码:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
        response.setContentType("text/html;charset=UTF-8");
        Calcular obj = new Calcular();
        obj.setNombre(request.getParameter("usuario"));
        obj.setDireccion(request.getParameter("direccion"));
        obj.setNumDiasTrabajados(Integer.parseInt(request.getParameter("dias")));
        obj.setValorDia(Double.parseDouble(request.getParameter("valor")));
        obj.Salario();
        
        request.setAttribute("ObjetoJava", obj);
        
        try (PrintWriter out = response.getWriter()){
            //out.println("Controlador");
            RequestDispatcher a=request.getRequestDispatcher("index.jsp");
            a.forward(request, response);
            
        }
    }

这是index.jsp中的代码:

<%
    Calcular obj = new Calcular(); 
    obj = (Calcular)request.getAttribute("ObjetoJava");
    if(obj != null){
        out.println(obj.getNombre());
        out.println(obj.getDireccion());
        out.println(obj.getNumDiasTrabajados());
        out.println(obj.getValorDia());
        out.println(obj.getSalario());
        
        
    }
 %>

这是在 Controlador.java 中发送的代码:

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
}

我做错了什么?

Controlador.java:

package ctr;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
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 modelo.Calcular;

/**
* Servlet implementation class Controlador
*/
@WebServlet("/Controlador")
public class Controlador extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
        response.setContentType("text/html;charset=UTF-8");
        Calcular obj = new Calcular();
        obj.setNombre(request.getParameter("usuario"));
        obj.setDireccion(request.getParameter("direccion"));
        obj.setNumDiasTrabajados(Integer.parseInt(request.getParameter("dias")));
        obj.setValorDia(Double.parseDouble(request.getParameter("valor")));
        obj.Salario();
        
        request.setAttribute("ObjetoJava", obj);
        
        try (PrintWriter out = response.getWriter()){
            //out.println("Controlador");
            RequestDispatcher a=request.getRequestDispatcher("index.jsp");
            a.forward(request, response);
            
        }
    }

/*  
private static final long serialVersionUID = 1L;
   
/**
 * @see HttpServlet#HttpServlet()
 */
public Controlador() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
}

}

index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="modelo.Calcular" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action = "Controlador" method = "POST">
 <table>
   <tr>
     <td>Nombre</td><td><input type="text" placeholder="Nombre" name="usuario"/></td>
   </tr>
   <tr>
     <td>Direccion</td><td><input type="text" placeholder="Direccion" name="direccion"/></td>
   </tr>
   <tr>
     <td># Dias Trabajados</td><td><input type="text" placeholder="# Dias Trabajados" name="dias"/></td>
   </tr>
   <tr>
     <td>Valor dias</td><td><input type="text" placeholder="Valor dia" name="valor"/></td>
   </tr>

   <tr>
     <td><button type ="submit">Calcular</button></td>
   </tr>

 </table>
 
 <%
    Calcular obj = new Calcular(); 
    obj = (Calcular)request.getAttribute("ObjetoJava");
    if(obj != null){
        out.println(obj.getNombre());
        out.println(obj.getDireccion());
        out.println(obj.getNumDiasTrabajados());
        out.println(obj.getValorDia());
        out.println(obj.getSalario());
        
        
    }
 %>
 
 </form>
 </body>
 </html>

您的表单方法调用 doPost:

当您点击提交时,它会转到“/Controlador”,在那里它会查找 doPost 并在 doPost 中调用 doGet 而不是调用 doGet,您应该调用:processRequest

试试这个:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException{
            response.setContentType("text/html;charset=UTF-8");
            Calcular obj = new Calcular();
            obj.setNombre(request.getParameter("usuario"));
            obj.setDireccion(request.getParameter("direccion"));
            obj.setNumDiasTrabajados(Integer.parseInt(request.getParameter("dias")));
            obj.setValorDia(Double.parseDouble(request.getParameter("valor")));
            obj.Salario();
            
            request.setAttribute("ObjetoJava", obj);
            
            try (PrintWriter out = response.getWriter()){
                //out.println("Controlador");
                RequestDispatcher a=request.getRequestDispatcher("index.jsp");
                a.forward(request, response);
                
            }
        }
    
    /*  
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Controlador() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
   response.getWriter().append("Served at: ").append(request.getContextPath());
    }
    
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response); 
    }