I'm trying to call a Servlet that writes the javaobject taken from database in json format from another Servlet.
The flow of my code is Servlet1 check_login -> Servlet2 jsonCreate
I'm getting HTTP 404 error when I try to do that.
Here is my check_login.java Servlet code
@WebServlet("/Check_login")
public class Check_login extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user_name=request.getParameter("user_name");
String password=request.getParameter("password");
try {
String role=check_database(user_name,password);
if(role.equals("")) {
response.sendRedirect("index.html");
}else if(role.equals("admin")) {
List<Programs> Programs_Offered = new ArrayList<Programs>();
RequestDispatcher rd = request.getRequestDispatcher("jsonCreate");
rd.forward(request,response);
}else if(role.equals("mac")) {
response.sendRedirect("mac_welcome.jsp");
}
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
And, here is jsonCreate.java Servlet code
@WebServlet("/jsonCreates.json")
public class jsonCreate extends HttpServlet {
public static List<Programs> list() throws SQLException, IOException {
List<Programs> Programs_Offered = new ArrayList<Programs>();
Connection conn=DataBase_Connection.getInstance().getConnection();
Statement ps=conn.createStatement();
ResultSet rs = ps.executeQuery(Queries.view_programs);
while(rs.next()){
Programs p=new Programs();
p.setProgramName(rs.getString("ProgramName"));
p.setDescription(rs.getString("Description"));
p.setApplication_Eligibility(rs.getString("Applicant_Eligibility"));
p.setDuration(rs.getInt("Duration"));
p.setDegree_Certificate_Offered(rs.getString("Degree_Certificate_Offered"));
Programs_Offered.add(p);
}
return Programs_Offered;
}
private static final long serialVersionUID = 1L;
public jsonCreate() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Programs> categories=null;
try {
categories = jsonCreate.list();
} catch (SQLException e) {
e.printStackTrace();
}
String categoriesJson = new Gson().toJson(categories);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(categoriesJson);
//response.sendRedirect("admin_welcome.jsp");
}
when I make the name of jsonCreates.json same as java servlet Name (jsonCreate) it runs fine and opens the json data on page at URL http://localhost:8081/servlet_demo/jsonCreate.
Then again when I re-direct to a new JSP admin_welcome.jsp it opens without any problem but I don't find any json data available in the link http://localhost:8081/servlet_demo/jsonCreate.
I'm commiting some mistake and I'm not able to find it. Can someone tell what's missing in this.
Aucun commentaire:
Enregistrer un commentaire