Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
Is it possible to call a servlet from jsp in windchill 10.0. I have my jsp page in location codebase\netmarkets\jsp\myfolder\myjsp. But from there I need to call my servlet which present in another location of my codebase.I have followed all the things to call a servlet from jsp but it is not working.Before that I had written my business logic in jsp itself but it throws an exception.So here I need a servlet to do that job.
Somebody help me to how to call a servlet from a jsp?Do I need to change the web.xml file in tomcat or what I need to do??
Solved! Go to Solution.
In addition to everything else, you probably want to make sure the servlet path is authenticated via the Apache auth confs. I would also change the url pattern to something a little less likely to interfere with possible OOTB mappings.
web.xml
<servlet>
<servlet-name>DownloadThing</servlet-name>
<servlet-class>mypackage.DownloadThing</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadThing</servlet-name>
<url-pattern>/servlet/mycompany/DownloadThing/*</url-pattern>
</servlet-mapping>
Apache auth.conf
<LocationMatch /servlet/mycompany(;.*)?>
form.jsp
<%
String cb = WTProperties.getLocalProperties().getProperty("wt.server.codebase");
String action = cb + "/servlet/mycompany/DownloadThing";
%>
<html>
<form action="<%=action%>">
</form>
</html>
It's unclear what exactly you mean by "calling" a servlet from a JSP.
You can forward a request to a servlet from a JSP. You can also include the response generated by a servlet in your JSP. Either should work just fine, and all the usual servlet API rules apply.
Yes I need to forward my request to servlet.Let me show what I have tried in jsp
<%@page import="wt.util.WTProperties"%>
<%@page contentType="application/octet-stream"%>
<%@page pageEncoding="UTF-8"%>
<%@ page language="java" import="java.io.*,java.net.*,java.util.*,javax.servlet.* "%>
<jsp:forward page="ext/bom/custom/Download" flush="true"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="Download" method="post">
</form>
</body>
</html>
From this I post this form action to the servlet called Download.It is present in the location ext/bom/custom/Download
But it is showing the error like the following
The requested resource (/Windchill/netmarkets/jsp/gt/ext/bom/custom/Download) is not available
It is looking only at the place the jsp is placed.How to call servlet then?Is my method is wrong??
Two things:
This is all basic JSP/servlet API stuff -- and isn't specific to Windchill in any way.
Thanks for the explanation.I tried as above by adding jsp:include also.It gives me same error.And I've studied we need to have servlet mapping for my servlet class.If that is the case where should I need to add the web.xml file??my jsp is in netmarkets folder and servlet class in different location.So where I need to add my web.xml.?
Also I tried adding my servlet and servlet mapping in codebase/WEB-INF/web.xml file.But it seems not working for me.And I read https://firstrobotics.ptc.com/Windchill-WHC/index.jspx?id=IEUG_WSFUseWSDeployBuildScript&action=show in the helpcenter about mvc.Do I need to follow those steps instead of this.??
It's still not clear what you are actually trying to do and until you correct your URL both includes and forwards will fail.
Any servlet will need entries in web.xml to be accessible. This again is general to servlets in general and not specific to Windchill.
But Where I need to place my web.xml in windchill directory??
You don't place your own web.xml in Windchill's directory. You're adding to the primary Windchill web app and thus editing its web.xml, which is found in WT_HOME/codebase/WEB-INF.
This is my jsp page
<%@page contentType="application/octet-stream"%>
<%@page pageEncoding="UTF-8"%>
<%@ page language="java" import="java.io.*,java.net.*,java.util.*,javax.servlet.* "%>
<%@page import="ext.bom.custom.Download"%>
<jsp:include page="E:\PTC\Windchill\codebase\ext\bom\custom\Download" flush="true" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="E:\PTC\Windchill\codebase\ext\bom\custom\Download" method="post">
</form>
</body>
and this how I have added servlet mapping in web.xml in codebase/WEB-INF/web.xml
<servlet>
<description>My download Servlet</description>
<servlet-name>Download</servlet-name>
<servlet-class>ext.bom.custom.Download</servlet-class>
<init-param>
<param-name>my-custom-download</param-name>
<param-value>/netmarkets/jsp/gt/get.jsp</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Download</servlet-name>
<url-pattern>/Download/*</url-pattern>
</servlet-mapping>
Please let me know if anything wrong here.This is not working for me.I need to forward the request to servlet called Download
You need an appropriate relative or absolute URL -- not an absolute file path.
You also need to figure out what you're really trying to do here. If you want the action of your form to be your download servlet then you shouldn't be also doing jsp:include of this. As far as the servlet mapping, my advice is the same that my first challenging math teacher always gave "monkey see, monkey do", i.e. follow the other numerous servlet examples.
In addition to everything else, you probably want to make sure the servlet path is authenticated via the Apache auth confs. I would also change the url pattern to something a little less likely to interfere with possible OOTB mappings.
web.xml
<servlet>
<servlet-name>DownloadThing</servlet-name>
<servlet-class>mypackage.DownloadThing</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadThing</servlet-name>
<url-pattern>/servlet/mycompany/DownloadThing/*</url-pattern>
</servlet-mapping>
Apache auth.conf
<LocationMatch /servlet/mycompany(;.*)?>
form.jsp
<%
String cb = WTProperties.getLocalProperties().getProperty("wt.server.codebase");
String action = cb + "/servlet/mycompany/DownloadThing";
%>
<html>
<form action="<%=action%>">
</form>
</html>
As far as I read to authenticate the servlet path can be done by specifying relative url of the resource by running ant call.I have made the above changes given by you in my form and web.xml.But the syntax for auth.conf what I have tried is different .
Here is what I've tried
E:\PTC\Apache>ant -f webAppConfig.xml addAuthResource -DappName=Download -Dresource=netmarkets/jsp/gt/get.jsp
Anyways the build is failed for this command.And I don't know how to use your command for apache auth.conf I need some on this. Thank you
the addAuthResource target is the supported way to go. Personally, if I were having trouble with it, I would just make changes to the two files manually. Add the path to the source authres.xml file so that it looks like all the rest, and add the path to the target auth.conf file so that it also looks like all the rest. Your call.
appName is probably "Windchill," not your servlet name. Make sure that your servlet path and any redirect path are authenticated, which it probably already is if it's under netmarkets/jsp.
Again, in my opinion, a servlet url of /Download is not a great idea. Not that it will necessarily ever happen, but if PTC ever creates a codebase/download folder, it may cause problems.
thanks.And if you are free can you please send me a code sample steps for calling a simple servlet from netmarkets jsp in windchill.Bcoz It's really hard for me to understand the ootb servlets flow
I think it's hard for everybody to understand the OOTB servlets and URLTemplateProcessor stuff. Good luck