1
\$\begingroup\$

I'm building a web application in Java. I'm new to Java, so before I get too far into this... I'm hoping to take advantage of all the experience here and learn the problems with this setup.

My Servlet

package cms.content;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Ben
 */
@WebServlet(name = "EditServlet", urlPatterns = {"/content/edit"})
public class EditServlet extends library.Controller {
    @Override
    public void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.loadView(request, response, "/content/edit");

        response.getWriter().write("Hi");
        System.out.println("EditServlet loaded");
    }
}

My Controller

package library;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Ben
 */
public class Controller extends HttpServlet {
    public final library.MySQL db;

    public Controller() {
        this.db = new MySQL();
    }

    public void loadView (HttpServletRequest request, HttpServletResponse response, String view)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher(view+".jsp");

        response.setContentType("text/html;charset=UTF-8");

        System.out.println("MyServlet::LoadView() success");

        dispatcher.forward(request, response);
    }
}

My View

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>This is edit.jsp</h1>
    </body>
</html>

I haven't tried to pass any parameters to this view or anything like that. But are there any obvious problems or improvements that need to be made?

\$\endgroup\$
1
\$\begingroup\$

First off, its not clear what you're trying to achieve, so its hard to evaluate.

But, looking a the code there comes after you forward to the view, that could be confusing, because it will still execute.

At a higher level, I also don't really see the need for this complicated of a setup to get to your edit page when you can just link straight to the edit jsp all nice an RESTful like. If you're just experimenting and trying to learn that's cool, but if you're really trying to get something done then you should look at just using an existing framework.

\$\endgroup\$
0
\$\begingroup\$
  1. Instead of printing messages with System.out you should use a logger framework (for example, SLF4J and Logback). See: log4j vs. System.out.println - logger advantages?

  2. The package in the public final library.MySQL db; is unnecessary, since the file is in the library package.

  3. library is not a good package name. See: Java Package Names on c2.com

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy