I am using ActiveMQ as my JMS provider and JSP to build up a real-time web application. The web application should subscribe to a topic produced in some other applications.
I have successfully deploy my we web application in Tomcat. Also, I have put all needed jar files in the lib directory. However, I still get an error, see the following error info from Google Chrome. Can someone tell me how to fix this problem.
My web.xml
<context-param>
<param-name>org.apache.activemq.brokerURL</param-name>
<param-value>vm://localhost</param-value>
</context-param>
<servlet>
<servlet-name>AjaxServlet</servlet-name>
<servlet-class>org.apache.activemq.web.AjaxServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AjaxServlet</servlet-name>
<url-pattern>/amq/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>MessageServlet</servlet-name>
<servlet-class>org.apache.activemq.web.MessageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MessageServlet</servlet-name>
<url-pattern>/message/*</url-pattern>
</servlet-mapping>
My index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Rainbow for Nurse Station</title>
<script type="text/javascript" src="amq/amq.js"></script>
<script type="text/javascript">
var amq = org.activemq.Amq;
amq.init({
uri: 'amq',
logging: true,
timeout: 20
});
var myHandler =
{
rcvMessage: function(message)
{
document.writeln(message);
}
};
amq.addListener("test", "topic://RAINBOW_NURSECALL", myHandler.rcvMessage);
</script>
</head>
<body>
None.
</body>
</html>
It seems you simply missed to include the following 2 scripts before amq.js:
<script type="text/javascript" src="<your_path>/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="<your_path>/amq_jquery_adapter.js"></script>
because org.activemq.Amq requires org.activemq.AmqAdapter etc.
Related
I am new to working with Spring and decided to follow this tutorial:
http://www.programcreek.com/2014/02/spring-mvc-helloworld-using-maven-in-eclipse/
My files and file structure match the tutorial, and index.jsp is working. However, when I click to go to helloworld.jsp, I get the following 404 error:
The origin server did not find a current representation for the target
resource or is not willing to disclose that one exists.
Can anybody suggest places to dig? Is there something wrong with the tutorial that is not suited to Tomcat 8.5? Or is it more likely that there is something wrong with my setup?
EDIT:
I have the following installed:
Tomcat 8.5.14
Eclipse Neon with Spring IDE
Maven 3.5.0
If it helps, Maven has been working before I tried using it with a Web/Spring (ie mvn install downloads the correct libraries)
I have included an image
of my files and a below is the actual code of the files that I believe are relevant:
UserController.java
package com.ankurmgoyal.hellotest.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
#Controller
public class HelloWorldController {
String message = "Welcome to Spring MVC!";
#RequestMapping("/hello")
public ModelAndView showMessage(
#RequestParam(value = "name", required = false, defaultValue = "World") String name) {
System.out.println("in controller");
ModelAndView mv = new ModelAndView("helloworld");
mv.addObject("message", message);
mv.addObject("name", name);
return mv;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
index.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC - HelloWorld Index Page</title>
</head>
<body>
<center>
<h2>Hello World</h2>
<h3>
Click Here
</h3>
</center>
</body>
</html>
helloworld.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 4 MVC -HelloWorld</title>
</head>
<body>
<center>
<h2>Hello World</h2>
</center>
</body>
</html>
dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.ankurmgoyal.hellotest.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
index.jsp is served by DefaultServlet. The url-pattern for dispatcher is /. / pattern overrides the defaultServlet and all the calls are routed through this servlet. Change the url pattern to some non-empty string and try it out.
Refer to this SO Question for more details.
I am developing a web application using Spring-MVC4
I configure my DispatcherServlet to handle all requests to '/'as below (web.xml) :
<servlet>
<servlet-name>WebAppConfig</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.xxx.config
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>WebAppConfig</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
my directory structure that looks like the following picture :
project directory structure
and my jsp file is as below :
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet"
href="/resources/css/Style.css" media="screen" />
<title>Insert title here</title>
</head>
<body>
<c:import url="/resources/common/menu.jsp" />
<h1>Home/Welcome page update</h1>
<p>
${message}
</p>
</body>
</html>
and finally my webapp configuration is :
#Configuration
#ComponentScan(value = { "com.xxx" })
#EnableWebMvc
#EnableTransactionManagement
#PropertySource("classpath:/app.properties")
public class WebAppConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
when i send my http request to the server, i can load jsp file in the / resources/common/menu.jsp but i can't handle css file !
when i send an http request to get css file like this http://localhost:8080/resources/css/Style.css i get 404 - Not Found error
Is there solution to this?
the probleme is in the jsp file when i call the css file with
<link type="text/css" rel="stylesheet"
href="/resources/css/Style.css" media="screen" />
the correct href is : href="<c:url value='/resources/css/Style.css' />"
I am new to JSF 2 and trying to build simple hello world app.Created maven project in Eclipse Luna with maven-archetype-webapp.
Following is my files :
web.xml :
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
HelloWorld.java :
#ManagedBean(name = "helloWorld", eager = true)
public class HelloWorld {
public HelloWorld() {
System.out.println("HelloWorld started!");
} public String getMessage() {
return "Hello World!";
}
home.xhtml :
<body>
<h1>HI....!!!</h1>
#{helloWorld.message}
<h:outputLabel value="#{helloWorld.message}" />
<h1>END....!!!</h1>
</body>
</html>
When i run, my home.xhtml prints HI....!!! and END...!!!,but nothing else.
I have searched in SO and googled. I found solutions but unfortunately, i am unable to solve it.
Some explanations on your current code:
#ManagedBean defines a managed bean to be used in Facelets by JSF. The attribute eager only applies for #ApplicationScoped beans, and your managed bean doesn't have such scope.
In JSF 2, if you don't define a scope for your bean, then it's #RequestScoped by default.
Looks like your html doesn't use any Facelets code. This is how your home.xhtml should be:
<!-- HTML5 header -->
<!DOCTYPE html>
<!-- define f: and h: usage in the current page -->
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<!-- JSF will include some js files for ajax functionality -->
<h:head>
<!-- this is optional -->
<title>Home</title>
</h:head>
<!-- use <h:body> for the content of your page -->
<h:body>
<h1>HI....!!!</h1>
#{helloWorld.message}
<!--
<h:outputLabel> will render a <label> and
this component should not be used to print
direct text on html but to label other HTML components
instead, use <h:outputText>
-->
<h:outputText value="#{helloWorld.message}" />
<h1>END....!!!</h1>
</h:body>
</html>
This is my code and i am using MyEclipse Juno with Apache Tomcat 7.0. and
I am using Jersey and i am getting this error "HTTP Status 500 - Servlet.init() for servlet Jersey REST Service threw exception ".
I have used #Path,#GET and #Produces with importing javax.ws.rs., javax.ws.rs.core.MediaType, javax.servlet. . Then anything I left..??? :-
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML
4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JAVA RESTful Demo</title>
</head>
<body>
JAVA RESTful Service
</body>
</html>
readme.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head><title>Read Me</title>
</head>
<body>
<h1>READ ME</h1>
</body>
</html>
web.xml
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.
container.servlet.ServletContainer</servlet- class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>REST</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
v1_status.java
#Path("/v1/status")
public class v1_status {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String returnTitle() throws ServletException{
return "<p>Java Web Servises</p>";
}}
I want to start work with Spring MVC, but can not adjust a simple example. I want to just pass one parameter form one jsp to another, but have error:
HTTP Status 404 - /controller/results.jsp type Status report message
/controller/results.jsp description The requested resource
(/controller/results.jsp) is not available. Apache Tomcat/7.0.12
My web.xml code:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
Servlet-context.xml
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="edu.demidov.controller" />
</beans:beans>
Controller.java
#Controller
public class HomeController extends HttpServlet{
private static final long serialVersionUID = 4825408935018763217L;
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
EducationDaoInterface educationDaoIntfc;
#RequestMapping(value="/home", method=RequestMethod.GET)
public ModelAndView firstActionPage() {
return new ModelAndView("home");
}
#RequestMapping(value = "/result.jsp", method=RequestMethod.GET)
public String SecondActionPage(#RequestParam String firstname, Model model) throws IOException {
model.addAttribute("myname", firstname);
return "result";
}
Result.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello form result.jsp
First name: ${myname}
</body>
</html>
Home.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="myform" action="<c:url value="/results.jsp"/>" method="GET">
<input type="text" name="firstname"/>
<input type="submit"/>
</form>
</body>
</html>
Thanks.
You have wrong URL mapping.
There are two solutions.
1.
You can change the url mapping of SecondActionPage function in your controller to following.
#RequestMapping(value = "/result", method=RequestMethod.GET)
And also change the form action in Home.jsp to <c:url value="/results"/>
2.
You can change the url-pattern of your appServlet to /*. That is the standard way to url-pattern in Spring web application.
And then you need to change the request mapping and action in home.jsp as I have suggested in solution 1.
I recommend you solution 2. That is best practice.
Hope this helps you. :)
try this
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
#RequestMapping(value="/home", method=RequestMethod.GET)
public ModelAndView firstActionPage() {
return new ModelAndView("home");
}
#RequestMapping(value = "/result", method=RequestMethod.GET)
public String SecondActionPage(#RequestParam String firstname, Model model) throws IOException {
model.addAttribute("myname", firstname);
return "result";
}
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form name="myform" action="/result" method="GET">
<input type="text" name="firstname"/>
<input type="submit"/>
</form>
</body>
</html>
You dont need to provide complete name of jsp file with extension as it will handled by org.springframework.web.servlet.view.InternalResourceViewResolver.
Change your form action to :
<form name="myform" action="result" method="GET"> //check spelling and remove '/' from url
And controller mapping to :
#RequestMapping(value = "/result", method=RequestMethod.GET)
Hope this may help you.
I have got this after all of the tying when I click on submit button from my home jsp to another controller url is:
`http://localhost:8080/controller/result?firstname=Vadim
it seems like I passing everything corectly to my controller but why my result.jsp page doesn't not appears
HTTP Status 404 - /controller/result
type Status report
message /controller/result
description The requested resource (/controller/result) is not
available.
Apache Tomcat/7.0.12