Need for web context - java

I need to use action="/WebConext/urlpattern" on one install of tomcat but only need to use "/urlpattern" on another install of tomcat. Why is this?
I am learning about Servlets using Eclipse and Tomcat. I have a form with action="/urlPatternOfServlet" and this worked fine for me. The url pattern was defined in servlet-mapping in web.xml.
I got a new laptop and ran the install of all environment as per previous laptop. Now I need to put the web context in the action path eg
I set up everything exactly the same and don't know why this is happening can anyone help with this.
The old laptop was running windows 8, the new machine is a VM running Windows server 2012 r2

check your web.xml in old system and make sure you have not missed web.xml with below settings
<servlet-mapping>
<servlet-name>yourserlvet</servlet-name>
<url-pattern>/WebConext/*</url-pattern>
</servlet-mapping>

Related

Project doesnt deploy to localhost root but deploys to localhost/projecttitle

Alternative title: Unable to find [main.SearchServlet], mapping my servlet in Tomcat whilst using annotations
I'm trying to migrate my web application from java netbeans glassfish jdk 1.8 to eclipse tomcat jdk 1.7.
I keep getting a 405 then 404 error when I try to perform my POST by pressing a button on my loaded index jsp.
javax.naming.NameNotFoundException: Name [main.SearchServlet/annj] is not bound in this Context. Unable to find [main.SearchServlet].
I thought that setting a <context root>/</context root> in web.xml would fix this. It did move my loading page from localhost/animelist/index and localhost/animelist down one level to localhost/ and localhost/animelist however it did not fix my error.
As it stands, my project does not run at http://localhost:8080/ or http://localhost:8080/index.jsp . It DOES run at http://localhost:8080/animelist1/ and http://localhost:8080/animelist1/index.jsp
The project works fine in my netbeans setup so my question is what do i need to do for my servlets to be mapped properly in tomcat?
Someone said that " Likely the only problem you have is that the application deployment in your secondary Eclipse isn't working properly yet. As in: the servlet class file is not deployed to the server at all." I'm not sure how to go about fixing this (or googling for it).
I have my classes instanced with
#EJB
private AnnJAXB annj;
and servlet with
#WebServlet(name = "SearchServlet", urlPatterns = { "/search" })
and like i said it all works in netbeans setup.
so do i have to do something with tomcat config or application properties, since deployment should be done automatically? why isn't mainClass.SearchServlet being found?
thankyou for reading. i am near the end i feel . it has been 15 hours trying to deploy this to a host and it is exhausting.

Switch java spring maven project from linux to windows

I am new to spring development so i am stuck here. Is there any configuration while switching spring + maven project from linux to windows. I am running on tomcat server 7. The project is initially developed in linux. I have moved all the file from linux to wondows. While running on server, i get error The requested resource (/myproject/login/) is not available. Do i need to make any changes or add add in configuation.
Thanks in advance.
I think you have missed something.
Try this.but i'am not sure this is working or not.
Tomcat, by default invoker servlet disabled (commented out in the web.xml file). You have to create a 'servlet' and a 'servlet-mapping' entry in your web.xml.
Once you do, you can get rid of the "servlet/" part of your url.
Check out the following URL for more information regarding the invoker servlet:
http://faq.javaranch.com/view?InvokerServlet

It does not work in tomcat servlet with eclipse

I have installed tomcat with eclipse and I have a project with a servlet. Here I put the structure of both:
Until this day the steps followed to run the servlet were:
1.Press button right above the following and give "Start"
Tomcat
2.Once booted, press right button over "UsuarioServlet.java" / Run as / Run on server
Done that everything worked properly but suddenly today when doing so I get the following:
What happened?.
I had this problem recently.
Here is what helped me.
Start tomcat and search in the console the real path where your tomcat is running.
Go there and open conf/server.xml. The plug-in that integrates tomcat and eclipse adds one long line there. Scroll down to see it. Here is how this line looks on my machine.
<Context docBase="C:\workspaces\work7\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\ROOT" path="" reloadable="false"/><Context debug="0" docBase="C:\workspaces\work7\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\webapp" path="/mycompany-webapp-0.9-SNAPSHOT" reloadable="true" source="org.eclipse.jst.jee.server:webapp">
<Valve className="org.apache.catalina.valves.AccessLogValve" pattern="common" prefix="access_log." suffix=".txt"/>
Please pay attention on path attribute. It used to be /webapp but now it became /mycompany-webapp-0.9-SNAPSHOT. So, this is the real URL where the application is deployed.
I used to surf to my application using url http://localhost:8080/webapp.
Now I have to use http://localhost:8080/mycompany-webapp-0.9-SNAPSHOT.
Alternatively I can stop server, fix the path in server.xml manually, start server and use the "old" URL.
The point is that this behavior is not consistent. Sometimes (when project settings are being changed) this problem is fixed "automatically" but sometimes it returns. It happened not only on my machine but on computers of 2 other team members in company where I work.
I hope this information will help you. Good luck.

How to set a parameter for a Java Web application

I have a web app in Java, which uses some external program (invokes a command line tool).
I want to make the path of the command line program configurable, so that I can change it without re-building my application.
Questions:
1) Which exactly parameter should I use (out of those available in web.xml), if it is set only once (at deployment) and thereafter never changes?
2) How can I access this parameter inside my Java code?
Thanks in advance
Dmitri
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<context-param>
<param-name>command</param-name>
<param-value>SOME_COMMAND</param-value>
</context-param>
.
.
.
.
</web-app>
Java code
String commandToExecute = getServletContext().getInitParameter("command");
Alternatively
You can also put this thing in property/xml file in the classpath read it and put it to servlet context when context initializes.
You may use an env-entry:
<env-entry>
<description>command line</descriptor>
<env-entry-name>commandLine</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>some_command</env-entry-value>
</env-entry>
And get it from anywhere in your webapp code:
javax.naming.Context ctx = new javax.naming.InitialContext();
String command = (String) ctx.lookup("java:comp/env/commandLine");
I would go with a system property in this scenario. Just run your application server with a JVM flag like -Dyour.command.path=/path/to/your/command and then in the code, you just need to write:
String cmd = System.getProperty("your.command.path", "/some/default/fallback/path/cmd");
This way you won't rely on running in some Java EE / servlet container.
It's a two part solution.
First we can make a properties file that is accessible for the web application. This need not be your standard message properties, but a secondary file.
Second your deployment script and your build script can do some extra work to create context directories in the application server where it can copy the properties file from the build and make it available for the command line tools also.
Apache CLI is a very good alternative to do some programmatic access.

unable to redeploy web application on glassfish3

I m trying to deploy a web application using netbeans to glassfish 3.01 but i m not able to do so
Glassfish says
Inconsistent Module State
the web app has some jars and a ejb jar for a local ejb
Open the domain.xml file that you will find in this folder:
"your glassfish installation"/domains/"your domain"/config
In this file look for your application deployment configuration to manually remove it:
1) Remove the web-module tag
<applications>
...
<web-module availability-enabled="false" context-root="yourapplicatoinContext" ... />
...
</applications>
2) Also remove the application-ref to your application:
<server config-ref="server-config" lb-weight="100" name="server">
...
<application-ref ... ref="your application name" ... />
...
</server>
Just try this:
undeploy
stop Glassfish
start Glassfish
deploy
If you can't undeploy, try the other steps anyway. If you don't have any luck, you could try it this way:
stop Glassfish
edit domain.xml (probably in domains/domain1/config)- back it up first; remove all references to your app (probably under application / application-ref); just make sure it's still valid xml
start Glassfish
deploy
It should have solved it. If still no luck anyway, remove every file or directory related to your app, probably under domains/domain1/generated and domains/domain1/applications/....
You could always recreate a new domain, but above steps will solve your problem.
You could be in such a situation after stopping Glassfish middle in a deploiyment, for example.
The other answers are really extreme. There is no need to create a new domain or re-install if you cannot redeploy your web application during development.
If you app is very complex, then consider scripting any clean up needed in a Production environment. The below steps will keep you up and working in the short term.
To re-deploy a web app to a single domain on Glassfish 3.1, do the following:
Stop Glassfish from the command line.
Delete all the files in the following directories:
glassfish\domains\domain1\generated
glassfish\domains\domain1\applications
Start Glassfish from the command line.
Deploy your app via the Admin Tool or command line.
In Glassfish 5, I searched for my app name in the domain.xml and deleted all the tags mentioning it. There was one occurence in the beginning (<system-applications>) and one at the end at approx. line 460 (<applications>). Then redeploy worked!

Categories