cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

We are happy to announce the new Windchill Customization board! Learn more.

How to call a servlet from jsp in windchill

VINO
3-Visitor

How to call a servlet from jsp in windchill

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??

1 ACCEPTED SOLUTION

Accepted Solutions
MatthewKnight
4-Participant
(To:VINO)

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>

View solution in original post

14 REPLIES 14
jessh
5-Regular Member
(To:VINO)

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.

VINO
3-Visitor
(To:jessh)

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??

jessh
5-Regular Member
(To:VINO)

Two things:

  1. Forwarding a request to a JSP/servlet results in that JSP or servlet "taking over" the response -- thus the rest of your JSP page after the jsp:forward will be ignored.
    • I'm not really sure what you're trying to do here. You could include some HTML generated by the servlet here via jsp:include, but that doesn't seem like what you're trying to do either. Your "form" element's action attribute should be a valid URL -- and I kind of suspect you want it directed to your custom download servlet.
  2. Your use of jsp:forward is using a relative URL -- specifically relative to the JSP page's location -- resulting in /Windchill/netmarkets/jsp/gt/ext/bom/custom/Download rather than the URL you intended.

This is all basic JSP/servlet API stuff -- and isn't specific to Windchill in any way.

VINO
3-Visitor
(To:jessh)

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.??

jessh
5-Regular Member
(To:VINO)

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.

VINO
3-Visitor
(To:jessh)

But Where I need to place my web.xml in windchill directory??

jessh
5-Regular Member
(To:VINO)

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.

VINO
3-Visitor
(To:jessh)

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

jessh
5-Regular Member
(To:VINO)

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.

MatthewKnight
4-Participant
(To:VINO)

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

MatthewKnight
4-Participant
(To:VINO)

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

MatthewKnight
4-Participant
(To:VINO)

I think it's hard for everybody to understand the OOTB servlets and URLTemplateProcessor stuff. Good luck

Top Tags