Skip to main content
12-Amethyst
April 1, 2013
Solved

How to call a servlet from jsp in windchill

  • April 1, 2013
  • 2 replies
  • 15165 views

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

Best answer by MatthewKnight

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>

2 replies

12-Amethyst
April 1, 2013

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.

VINO12-AmethystAuthor
12-Amethyst
April 1, 2013

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

12-Amethyst
April 1, 2013

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.

1-Visitor
April 2, 2013

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>

VINO12-AmethystAuthor
12-Amethyst
April 3, 2013

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

1-Visitor
April 3, 2013

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.