Issue
I'm trying to send variables between jsp using servlets, but I got this error that I still can't figure out why. It keeps sending HTTP Status 500 error.
Here's the jsp:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
Fuzzy Topsis Supplier Selection
</title>
<!-- Normalize -->
<link href="assets/css/normalize.css" rel="stylesheet">
<!-- Bootstrap -->
<link href="assets/css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap -->
<link href="assets/css/custom_style_00.css" rel="stylesheet">
<!-- Font -->
<link href="assets/css/font.css" rel="stylesheet">
</head>
<!-- BODY STARTS HERE -->
<body>
<section class="main_wrapper">
<%
request.getSession().invalidate();
%>
<!-- FORM STARTS HERE -->
<div class="form_wrapper">
<form action="./Page_One_Controller">
<div class="sub_form_wrapper">
<span>
Input jumlah supplier :
</span>
<div class="sub_form_input">
<input type="text" class="form-control" name="sNum">
</div>
</div>
<div class="sub_form_wrapper">
<span>
Input jumlah pengambil keputusan :
</span>
<div class="sub_form_input">
<input type="text" class="form-control" name="dNum">
</div>
</div>
<div class="sub_form_wrapper">
<span>
Input jumlah kriteria :
</span>
<div class="sub_form_input">
<input type="text" class="form-control" name="cNum">
</div>
</div>
<div class="sub_form_wrapper">
<div class="sub_form_input">
<input class="btn btn-default btn-lg" type="submit" value="Submit" name="submit">
</div>
</div>
</form>
</div>
</section>
</body>
And here's the servlet :
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;
@WebServlet(name = "Page_One_Controller", urlPatterns = {"/Page_One_Controller"})
public class Page_One_Controller extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
int sNum = (Integer)request.getSession().getAttribute("sNum");
int dNum = (Integer)request.getSession().getAttribute("dNum");
int cNum = (Integer)request.getSession().getAttribute("cNum");
request.getSession().setAttribute("sNum", sNum);
request.getSession().setAttribute("dNum", dNum);
request.getSession().setAttribute("cNum", cNum);
request.getRequestDispatcher("page_two.jsp").forward(request, response);
}
}
Sorry for the long post, it would be great if anyone can shed some light.
Thanks.
Solution
The reason why you get the 500 error is in this line:
request.getSession().setAttribute("sNum", sNum);
Here you try to set a session attribute to a not initialized primitive datatype.
To fix that should be getting the value from the request Object and not from session:
int sNum = Integer.parseInt(request.getParameter("sNum"));
So your servlet code should be somthing like this:
int sNum = Integer.parseInt(request.getParameter("sNum"));
int dNum = Integer.parseInt(request.getParameter("dNum"));
int cNum = Integer.parseInt(request.getParameter("cNum"));
request.getSession().setAttribute("sNum", sNum);
request.getSession().setAttribute("dNum", dNum);
request.getSession().setAttribute("cNum", cNum);
request.getRequestDispatcher("page_two.jsp").forward(request, response);
Answered By - MaVRoSCy