Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have made a java program that allows to mark an exam made by one student. In the interface the student chooses between a set of options for each question, and by an array (that holds the correct answers) I get the score of the student. By the way this simple program is made using console java, no swing or applets at all.

The information of which answers are correct are stored in a vector, so each answer of the students is compared with this vector for getting the final score.

I would like to put his program into one domain that I recently got, so that one student at the moment of entering into the webpage could make the exam; and another program installed in the server will check the correct answers and give other questions, in case the student wants to practice.

One friend told me that it is possible to wrap the console java application into java for web, but I just dont know the details for doing that.

What would anybody suggest or if there is any online tutorial that could help me with that?

My domain has the following extensions:

  • FTP
  • MySQL
  • Apache
  • SharedTomcat

Please this is not a discussion to know which tool is better, I want just to use Java, but I am pretty lost with all the web technologies that one can use with this language.

share|improve this question

2 Answers 2

You could use the Dropwizard framework

Dropwizard provides a complete web application environment, including servlet container, which is deployed as a single executable JAR file.

For example you could deploy a Dropwizard application to, say, Heroku or Amazon EC2 and have the application respond directly to web requests. Here is a tutorial. This is probably a lot simpler to get running than the complexity inherent in JEE applications with all their configuration.

How console and web applications are different

As @MichaelT has mentioned in his article you will need to make some modifications to your code to make it a web application and this is not trivial. Almost without exception, console applications retain state between input and output from the user. Web applications do not so you are fundamentally changing the architecture of your code.

Think of it like having a single console running your application and many users all sharing the keyboard (they can take turns but you can't predict who's turn it is). Somehow you have to ensure that user1 is only able to mark exams, but user2 can only create exams and so on. Essentially after every input to your console app the screen resets back to the start so any user can step up and start work on the keyboard. Clearly the first entry they make must be an identifier so that user1 can jump quickly to where they were a moment ago before user2 got their hands on the keyboard. This is the role of session IDs in web applications, and they typically get handled automatically by the browser in a cookie.

Introduce layers to expose common code

To modify your existing application to turn it from a console app into a web app you need to split your console app into some layers that represent shared code. For example, once you have a user identified you will call into some data retrieval logic that connects to a database and gets the exam that they will work on. This code would be common to both the console and the web applications, so could be moved into a shared JAR.

Eventually you'll find that you have code that just handles the user interactivity (the UI layer), another JAR that handles special processing logic such as data validation and collating exam results (the business layer) and yet another JAR that just looks after data persistence and retrieval (the data layer).

Create the web-specific layer

Once you reach this point, you can create a specific web UI (the wrapper that your friend is talking about) and hook it up to the business and data layers JARs just by including them in your build. The web layer will contain views that output HTML for rendering by a browser, and it will have endpoints that will retrieve exam data in response to a GET request or add new exam data in response to a POST request.

Overall, this would be a good exercise for your personal development as a programmer so definitely persevere with it.

share|improve this answer

Your friend is drastically oversimplifying the solution as "wrap the console java application into java for web."

You have two basic approaches to maintain a java code base for the system. One is far, far, less than ideal and very few people do it. The other takes some work.

The far, far, less than ideal approach is to make a CGI in the Apache side of the house that launches a full java application. CGI passes in some information via standard input and/or environment variables. The standard output of the program is then treated as a web page (one must also be sure to add all the appropriate headers and such).

The other approach is to make a java servlet on the tomcat side of your stack. A servlet is an application that is hosted within an application server (thats what tomcat is). It takes a request and a response and ultimately builds a web page through a JavaServer Page (one can do it with System.out, but again, it is far less than ideal).

There are many, many tutorials out there on "tomcat for beginners" and "simple jsp/servlet" (including some linked from the Wikipedia page linked above).

See also What are some good books on Java Servlets and JSP?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.