I have a Webapp running on Tomcat. This app has an entry Servlet which receives a certain parameter. It looks like this:
https://localhost:8443/myapp/entryservlet?param=app1
This parameter param indicates which webapp should be loaded.
In my Webapp directory I have a structure like this:
-WebApp
-- App1
-- App2
-- App3
...
Depending on the parameter, I have to tell Tomcat which webapp should be loaded
Obviously, you're not passing in the entire URL as a parameter, but here's a sample redirecting to another url. You can also look at forwarding.
public class EntryServlet extends HttpServlet {
public final void doGet(HttpServletRequest request, HttpServletResponse response
) throws ServletException, IOException {
String forwardURL = request.getParameter("param");
response.sendRedirect( forwardURL );
}
}
Related
I am trying to re-package a relatively big java webapp which I did not code and for which the project configuration was lost.
I setup a first packaging, and deployed it in tomcat. Now to understand it, I'd like to get a list of the servlets that started successfully or failed, with corresponding access url.
Is there a way to get that list (from some startup log maybe)?
Some details: the webapp uses gwt (which I don't master), I use tomcat7 on ubuntu. I am not against a solution using another servlet container, if practical.
I would write a simple JSP or ServletContextListener to read all the ServletRegistratioins from the servlet context and display them.
So your JSP/ServletContextListener would read the data from
servletContext.getServletRegistrations();
and just display it.
Edit
#WebServlet(urlPatterns = "/mappings")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = -7256602549310759826L;
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
Map<String, ? extends ServletRegistration> registrations = req
.getServletContext().getServletRegistrations();
for (String key : registrations.keySet()) {
ServletRegistration registration = registrations.get(key);
writer.write("Name: " + registration.getName());
writer.write("<br>Mappings:");
for (String mapping : registration.getMappings()) {
writer.write(mapping);
}
}
// of course you can write that to log or console also depending on your
// requirement.
}
}
You can see started modules and paths in Tomcat by double click at Tomcat and see Modules tab.
I have an JavaEE Application that has 2 web applications. I also have another library web module that contains common_bean that annotated by #ApplicationScoped
My question is: Can I share common_bean instance across the two web applications?
Updated - I did the test
In Web App1 (/web1)
#WebServlet("/Servlet1")
public class Servlet1 extends HttpServlet {
#Inject
CommonBean commonBean;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
commonBean.setValue("Servlet1: " + System.currentTimeMillis() + "--" + commonBean);
}
}
In Web App2 (/web2)
#WebServlet("/Servlet2")
public class Servlet2 extends HttpServlet {
#Inject
CommonBean commonBean;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
commonBean.setValue("Servlet2: " + System.currentTimeMillis() + "--" + commonBean);
}
}
The Result
If I run /Web1/Servlet1 FIRST then Run /Web2/Servlet2:
/Web1/Servlet1 ------- Worked
/Web2/Servlet2 ------- Failed with CDI exception
If I run /Web2/Servlet2 FIRST then Run /Web1/Servlet1: ( Restart server then re-test)
/Web2/Servlet2 ------- Worked
/Web1/Servlet1 ------- Failed with CDI exception
Any comments!
I found the issue. I like to post the solution here. It may help someone:
The solution is: Configure the Web library module as EAR Module Assembly (Lib jar module) - By doing this, only instance of Common bean created and this instance will be shared across all web applications in the same EAR.
I am not sure this is a specification of CDI or NOT but it worked on both Glassfish & Wildfly.
Post on Liferay Forums: https://www.liferay.com/community/forums/-/message_boards/message/47412302
I have a simple application setup within a JSR-286 portlet to retrieve the value from a Portlet session.attribute
doView() method:
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws PortletException, IOException
{
renderResponse.setContentType("text/html");
getFormBean(renderRequest.getPortletSession());
PortletURL renderUrl = renderResponse.createRenderURL();
renderUrl.setWindowState(WindowState.MAXIMIZED);
PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(this.viewUrl);
dispatcher.include(renderRequest, renderResponse);
}
I set my attribute here in
TestPortlet.java:
private void getFormBean(PortletSession session)
{
String testVar = (String)session.getAttribute("testAttr", 1);
if (null == testVar) {
System.out.println("Setting Attribute inside Portlet");
session.setAttribute("testAttr", "TESTING SESSION", 1);
}
}
And retrieve the attribute here in TestServlet.java (same package):
private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String testVal = (String) request.getSession(true).getAttribute("testAttr");
System.out.println("Test Attribute from Servlet:"+testVal);
}
The output of the application returns null
Setting Attribute in Portlet
Test Attribute from Servlet:null
Output should be:
Test Attribute from Servlet:TESTING SESSION
This application does work on my local setup, however not on a remote server with almost the same configurations.
I've included the javax-servlet-api-3.1.0 in my tomcat/lib to retrieve the HttpServletRequest Class, haven't found what else could be missing. I also haven't seen any Exceptions/ClassNotFound Errors.
Could there be any kind of server configuration that could interfere with the Session? (Authentication, network config, security)
Local setup
Tomcat 7.0.33
jdk-1.7 (compiled with 1.6 and 1.7)
Remote setup
Tomcat 7.0.33
Apache Web Server
jdk-1.6.0u35
more jar files in /lib (jdbc drivers, etc)
If you want to share session data between portlet and servlet in the same application (war), you have to place the attribute in the application scope, like this:
portletSession.setAttribute("testAttr", "TESTING SESSION", PortletSession.APPLICATION_SCOPE);
and then also retrieve it in portlet using scope:
portletSession.getAttribute("testAttr", PortletSession.APPLICATION_SCOPE);
how can I add a robots.txt file to a Vaadin application?
I found nearly nothing related, but what I found states that there is no support for such a file.
I'm using Vaadin 7.1.1 with JBoss 7.1.1 and Vaadin-CDI-Integration.
My workaround approach is: By adding RobotsUI to the project, the URL http://localhost:8080/App/robots.txt becomes accessible.
#CDIUI(value="robots.txt")
public class RobotsUI extends UI {
#Override
protected void init(VaadinRequest request) {
// Send a response with mimetype
// `text/plain` with self defined content.
}
}
My problem is: How can I deliver a self-edited, text/plain response?
Thanks for any help :-)
I successfully published text/plain by adding a common HttpServlet to the project:
#WebServlet("/robots.txt")
public class RobotsServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("Text...\n");
}
}
Do it outside of Vaadin, register a filter before vaadin servlet and in case of robots.txt uri return your robots file. Or add some static resource serving servlet registered lets say to /static/* and bind your /robots.txt redirect with UrlRewrite.
My dynamic web module version is 3.0 and tomcat my tomcat version is 7.0.
I don't know why it can't see my servlet XD when I navigate to the page localhost:8080/SimpleProject.
When I navigate to localhost:8080/SimpleProject/firstservlet, I get the error message that says the requested resource is not available!
Here's my servlet:
#WebServlet(description = "the first servlet", urlPatterns = { "/firstservlet" })
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("<h1>Why can't See<h1>");
}
I didn't do anything on my web.xml because what I know is in 3.0 version the url mapping is in the servlet.
this is my file arrangement :)
Have you check the log files to see if some exception is being thrown when loading/executing your servlet FirstServlet?
Did you check the content of your war file to make sure it is packaged properly and the servlet class is in it (you can open a war file like you would a zip file).