This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem occuring in calling servlet

 
Rushabh Vashi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anybody tell me how to call servlet from jsp page?

i have done with mapping in web.xml file and i also put action="/Urlpattern" into form tag of JSP.....still 404 error is coming....

what to do?
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, you make certain actions be handled by struts, by forwarding all actions that has a specific suffix. What suffix are you using? It is like:

j

If .do is your suffix, your action should be:

action="/Urlpattern.do"
 
Rushabh Vashi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesus Angeles

Thank you for reply....
i am using struts2 form my application.
And i haven't use any suffix for my Servlet.
i just try to call it from JSP page...
can you give some suggestion???

Thank you in advance
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paste your web.xml and struts.xml... use code tags...
 
Rushabh Vashi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<interceptors>
<interceptor name="interceptorTest" class="example.InterceptorEx">
</interceptor>
</interceptors>

<package name="default" extends="struts-default">
<action name="HelloWorld" class="example.HelloWorld">
<!-- <interceptor-ref name="interceptorTest"></interceptor-ref> -->
<result>/example/HelloWorld.jsp</result>
</action>

</package>

</struts>

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>ServletTest</servlet-name>
<servlet-class>ServletTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletTest</servlet-name>
<url-pattern>/ServletTest</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>



MyJspPage
<form action="/ServletTest" method="post">
<input type="submit">
</form




Do i neeed to map servlet into struts.xml also??
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rushabh Vashi wrote:Struts.xml
.....................

<package name="default" extends="struts-default">
<action name="HelloWorld" class="example.HelloWorld">
<!-- <interceptor-ref name="interceptorTest"></interceptor-ref> -->
<result>/example/HelloWorld.jsp</result>
</action>

</package>
..........
........
......
.....


MyJspPage
<form action="/ServletTest" method="post">
<input type="submit">
</form




Do i neeed to map servlet into struts.xml also??







This statement -> <form action="/ServletTest" method="post"> means you are attaching the form to that "Action" defined in the struts.xml

you can do this -> <s:form action="HelloWorld.action" method="post">

and what do you want to achieve by calling a servlet from a jsp ?

you can always define the startup sequence of a servlet if you want to initialize / load configurations using that servlet..



.... but all you want to do in a jsp is go back to the action defined in the struts.xml ...

[Edited post for fixing typo]
 
Rushabh Vashi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robin John

Thank you for the reply...

i just want to know that how we can call servlet through JSP in Struts2 Application.

I have created simple web application.in that i create One JSP file and one Servlet. Now if i try to call servlet from JSP i can do using <form action="./UrlServlet">...but if i try to migrate this code with my application it shows following error

HTTP Status 404 - There is no Action mapped for namespace / and action name ServletTest.

i have pasted my web.xml as well as index.jsp file in my previous post.

Sugggest me what should i do...

Thank You in advance:
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

HTTP Status 404 - There is no Action mapped for namespace / and action name ServletTest.


Thats because, it tries to map the url action name to the action present in struts.xml but in your case there is no mapping for that servlet....

anyways.. in struts2 you can make any POJO as an action class so try to map the servlet in your struts.xml file and have an execute method.. but that might defeat the purpose of a 'servlet'...

Again, I recommend to use servlets / interceptors for preprocessing instead of using them as an action class.

struts.xml
 
Rushabh Vashi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robin John

Thank you for replying...

I have tried your code also but still it is not working....

and bro as i am new to struts2, can you please tell me why it is not appropriate using servlets in application?

As i know in struts2 MVC architecture is achieved by Filter,Action and Results....
now i am doing all controlling related thing using Actions classes only which i have declared in struts.xml....
so what is exact use of Filters in Struts2???

In normal MVC architecture we are using Servlets as a controller then cant we use them in struts2???
 
Robin John
Ranch Hand
Posts: 281
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its working for me... Dont know why its not working for you...

I didn't mean to say that you "can't" use servlets... but using servlets as an action is not the right idea.... thats all...

and for the configuration ... let me know what problem are you facing ?
 
Rushabh Vashi
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey...

I resolved that problem.
I actually called that servlet using its URL pattern in the struts.xml file only...

here is that code of action in struts.xml

<action name="ServletCallingAction" class="example.ServletCallingAction">
<result>/ServletCallingActionServlet</result>
</action>

and the servet is actully getting the input page as its previous page....

m happy...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic