I'm trying to create a servlet and deploy it to the SAP cloud platform. Not familiar with servlets at all.
To start, I have created and deployed what is pretty much whatever eclipse generates as their example app
#WebServlet("/hello")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public HelloWorldServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().println("Hello World!");
}
}
Then, I deploy this and I can see the app is running and I have a URL.
I'm trying to access it from the browser now, by the URL you see in the screenshot above. I thought that all I had to do was add /hello to the end of it and we're off. It gives a 404 though. The access logs say I'm accessing that servlet but I cannot get the "hello world" to show:
Anyone know what the URL's are meant to be here?
Edit:
On redeployment, it starts to chuck errors. Only the initial deploy seems to work:
Problem during deploymentJava version [8] is not compatible with the
currently specified runtime; use runtime neo-java-web 2.x or 3.x or
neo-javaee7-wp 1.x
The answer was not to use the "deploy to sap cloud platform" options in Eclipse, but to export as WAR file and choosing Tomcat 8 as the runtime environment. After that, manually update the application on SCP by uploading the WAR file and restart the running process.
Worked for me.
Related
Programming a website with PHP is very easy as it always gives back an HTML response which the browser can send to the user. But how to release a website with java? I'm using Aruba.it hosting, I created a file containing java code, actually a simple servlet with this code;
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("/Demo")
public class Demo extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("Do get called");;
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("Do post called");
}
}
However, when I visit www.mywebsite.com/Demo.java or simply /Demo, I get the code I showed you before;
I know this is a very stupid question, but I have no idea what to do. When I run a PHP file everything works as expected. What would you do?
What you are asking is indeed a beginner question. Most webhosting providers (like aruba.it) base their Linux hosting services on Apache httpd server (better known as just Apache) which is a web container and supports php out of the box. However Java servlets need a servlet container like Apache Tomcat. For setting up a servlet container you need to have a VPS rather than a shared Linux hosting.
Secondly the code you have provided is actually a servlet which needs more elaborate steps and configuration to deploy. Please see this tutorial https://www.tutorialspoint.com/servlets/servlets-packaging.htm
If you are looking for something similar to PHP then consider using JSP (Java server pages) which allow you to embed html. Even so JSP still requires a servlet container like Tomcat to deploy (https://www.codejava.net/servers/tomcat/how-to-deploy-a-java-web-application-on-tomcat). Just that once initial set up is done, with JSP you can just put them in the correct location and call them like a php page and expect them to get compiled and run automatically rather than going through the more elaborate steps for setting up a servlet
I've built a java program that logs into a game server and asks the user for an input ID and then sends a packer to the game server and parses and prints out the reply.
I need to convert this into an API that will be run on Tomcat I assume? I've installed Tomcat on my server but I am not sure what to do now and what the correct way to convert this would be.
Any help would be greatly appreciated.
I have done similar things in the past. Mainly turn regular applications into web applications. Or more simply put, wrapping an application inside a Servlet web application so it can be controlled via an HTTP API.
By the way there are multiple ways to do this. Many in fact. This is just one way. Servlets is a Java interface that allows developers to quickly create a server, mainly an HTTP server, though its not limited to just that. Tomcat is a Servlet container. That means you can create a Servlet, then register it to the Servlet container, either using a special file called a web.xml or annotations. In the example below I use the WebServlet annotation to register my Servlet with Tomcat. Once registered, Tomcat will send any requests destined to the your application (the name of the WAR file) and to the specific Servlet (the registered urlPatterns, see example below). So if your WAR file is named "MyWebServer", and your Servlet has registered the urlPattern "getSomething", then any requests to the Tomcat with the URL MyWebServer/getSomething, will be directed to your Servelet's doGet or doPost command. If you do a simple browser request (or curl on Linux) with no msg body, then by default its a HTTP GET request. Stick with GET requests for now until you get the hang of it.
A couple of things you will need.
1) Know how to package a Java application into a WAR file. The WAR file would then just go to into Tomcat's webapp folder. When Tomcat starts up it will unzip the WAR files in that directory and host the web application implemented in that WAR file. Please read up on WAR files. The easiest way to do it is to use your IDE, I prefer Netbeans, and click on new project. In Netbeans you can select new Maven->Web Application project -or- Java->Web Application. It will set up the directory structure.
2) You will need a Servlet. Create a Servet, please read up on it. Once again your IDE will make things easy for you. In Servlet 3.x you can configure your Servlet using annotations. Your Servlet will actually be a HttpServet. Here is a simple example, you will have to look up the various components (like HttpServletRequest and HttpServletResponse) to get the full blown details. Stackoverflow has a treasure trove of information on how those two work.
#WebServlet(name = "MyWebServer", urlPatterns = {"/getSomething", "/postSomething"}, loadOnStartup = 1)
public class MyWebServer extends HttpServlet {
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//Get the input to your web service from the HttpServletRequest.
String someParam = request.getParameter("someParamName");
//process your request
String output = yourExistingClass.processSomething(someParam);
//set the response
try (PrintWriter out = response.getWriter()) {
out.println(output);
}
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
///////////////////////////////////////////////////////////////////////////
//public functions
#Override
public void init() {
//put any code you want executed when the application comes up in here.
}
#Override
public void destroy() {
//put any code you want executed when the application comes down in here.
}
}
3) You will need to include your JAR in your application. Once again your IDE can help you add your JAR to your project. Your JAR, and any other dependency JAR's will get packaged inside your WAR, specifically inside the WEB-INF/lib folder.
I hope this helps get you started.
This is the sample servlet that I wrote, nothing fancy:
#WebServlet("/SimpleServletPath")
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("Hello");
}
}
I am using external installation of Tomcat v8.0 on the computer, which gives correct page on querying http://localhost:8080/.
The Dynamic Web Module being used in Eclipse Luna is 3.1. Also since I am using the #WebServlet annotation, I have not generated or making use of a web.xml file.
No matter what I do, Tomcat is always giving me error on running the Servlet.
HTTP Status 404 - /SimpleServlet/SimpleServletPath
type Status report
message /SimpleServlet/SimpleServletPath
description The requested resource is not available.
--------------------------------------------------------------------------------
I know it sounds silly, but I had unchecked Build Automatically in Eclipse. And I guess that was preventing any changes to my project from being built and deployed to the Tomcat.
Just checked Build Automatically and now everything works flawlessly.
Servlet 3.1 (with/without web.xml) as well as Servlet 2.5 (with web.xml).
I have been trying for 3 days now to create a Java EE project, that uses JSP, Servlet and EJB in a single project, as I need to do a course final assignment on this.
We were instructed to use JBOSS 4.2.3, and so that is what I try to use.
I set up my environment as follows:
http://community.linuxmint.com/tutorial/view/1372
Install IntelliJ Idea 13 Ultimate.
Download JBOSS and prepare a directory to use.
After those 3 days of hard work, I managed to get a sample app, created by this tutorial:
http://wiki.jetbrains.net/intellij/Developing_and_running_a_Java_EE_Hello_World_application#The_Hello_World_Java_EE_application
Now, the application compiles, I get the index.jsp on: http://localhost:8080/webWeb/
However, I get a 404 error if I click it, and submit to http://localhost:8080/webWeb/helloworld
I dont know what else to try, I think I Googled and read pretty much everything :(
Here is a link to the Project archive, so that you could (potentially) test out my project, see if you can solve this problem somehow...
https://www.dropbox.com/sh/9sma5vh7usy2h3p/AADA64KPyLH29iGz8OamWyNna
Thanks!
UPDATE:
For convenience, my HelloWorldServlet.java code:
package myservlets;
import mybeans.HelloWorldBean;
import javax.ejb.EJB;
import java.io.IOException;
#javax.servlet.annotation.WebServlet(name = "HelloWorldServlet", urlPatterns = "/helloworld")
public class HelloWorldServlet extends javax.servlet.http.HttpServlet {
#EJB
private HelloWorldBean helloWorldBean;
protected void doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, IOException {
}
#Override
protected void doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException, IOException {
String hello=helloWorldBean.sayHello();
request.setAttribute("hello",hello);
request.getRequestDispatcher("hello.jsp").forward(request,response);
}
}
I feel I really must add, that this extreme difficulty of setting up Java to just work and allow me to focus on CODING, is the reason I prefer something like PHP, which just works for example... Am I wrong?
The problem is that you're using JBoss 4.2.3 which works with Servlet 2.5 / JSP 2.1 (as noted by BalusC here: Servlet Spec for Jboss 4.2.3). Usage of #WebServlet and annotations is supported since Servlet 3.0. So, you have to configure your servlets directly in web.xml file.
Note that servet 2.5 doesn't support EJB injection either.
I have a pre-existing Java application and I would like to expose a web UI using Vaadin. I'm using Maven for dependency management.
The Vaadin documentation suggest using a war file layout, but I don't want to have to rearrange my codebase into the standard War format.
Is there a way that I can programmatically start a Jetty server and get it to serve up a servlet, without having to worry about war directory structures?
Some example code showing how to serve up a servlet from a main() method would be very helpful here.
Alternatively, if something other than Jetty would work better here, that would be good to know.
It is pretty straightforward to set up a simple HTTP server in-process with jetty:
final Server httpServer = new Server(18080);
httpServer.setHandler(new AbstractHandler() {
#Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.getWriter().write("This is the HTTP response");
}
});
httpServer.start();
Note that this is based on jetty 8.1.8. The code above does not use Servlets, but it is pretty easy to wire it to any framework you want.
If you really need a servlet (maybe you already have it ready) use Jetty's ServletContextHandler class instead of your own handler.