Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to print XML from servlet?

 
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I am trying to print xml from a servlet, but when I run the servlet, I can only see an empty page. Please suggest.

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Swati,
If you had a System.out.println in the servlet, does it get output to the logs? In other word, does the servlet actually get called?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Do a View Source at the browser. What do you see?
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I tend to get confused on this, but I think that calling close() on the printWriter causes a close of the response writer and I don't think that you're supposed to do that.

Also, be aware that a lot of browsers have "XML magic" in them so that they can accept XML and display/format it in various interesting ways. So try switching Content-Type to "text/text" and if the XML shows up, then you can switch back to "text/xml" and make adjustments.
 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Tim Holloway wrote:I tend to get confused on this, but I think that calling close() on the printWriter causes a close of the response writer and I don't think that you're supposed to do that.

Also, be aware that a lot of browsers have "XML magic" in them so that they can accept XML and display/format it in various interesting ways. So try switching Content-Type to "text/text" and if the XML shows up, then you can switch back to "text/xml" and make adjustments.



I made the following changes as suggested. And I still see nothing.

 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

swathi bairu wrote:I am trying to print xml from a servlet, but when I run the servlet, I can only see an empty page. Please suggest.



Thank you all for your help. I had another servlet in the project with the same URL pattern (WelcomeServlet), so that one was being called. I changed that to something else and now I can see the xml response on the browser.
I
 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Bear Bibeault wrote:Do a View Source at the browser. What do you see?



I have a working RESTEasy service deployed to JBOSS EAP 6.3. It prints an XML to the browser. I am calling this service from a servlet, but I see nothing on the browser when I run this servlet in eclipse. When I run this servlet on a normal browser (mine is chrom) it is giving the following message:

This page contains the following errors:

error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.

When I open the browser's "view page source" - I can see the XML response here.

Testing...
Good Response

*** Response from Server ***

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><employee><empAddress>San Antonio TX</empAddress><empId>1111</empId><empName>EmployeeOne</empName></employee>

My Servlet



Please Suggest.
 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

swathi bairu wrote:

Bear Bibeault wrote:Do a View Source at the browser. What do you see?



I have a working RESTEasy service deployed to JBOSS EAP 6.3. It prints an XML to the browser. I am calling this service from a servlet, but I see nothing on the browser when I run this servlet in eclipse. When I run this servlet on a normal browser (mine is chrom) it is giving the following message:

This page contains the following errors:

error on line 1 at column 1: Document is empty
Below is a rendering of the page up to the first error.

When I open the browser's "view page source" - I can see the XML response here.

Testing...
Good Response

*** Response from Server ***

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><employee><empAddress>San Antonio TX</empAddress><empId>1111</empId><empName>EmployeeOne</empName></employee>

My Servlet



Please Suggest.



When I changed "request.accept("application/xml");" to "request.accept("text/xml");", it started working fine.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

swathi bairu wrote:When I open the browser's "view page source" - I can see the XML response here.

Testing...
Good Response

*** Response from Server ***

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><employee><empAddress>San Antonio TX</empAddress><empId>1111</empId><empName>EmployeeOne</empName></employee>



That's because that's not XML.
You have a whole load of spurious text at the front of it.

The reason that text/xml works is because it's far more lenient.
You shouldn't be relying on that, though.
If you should be proper XML, then it should be application/xml, and if it's rejected then your XML is incorrect and should be fixed.
 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Dave Tolls wrote:

swathi bairu wrote:When I open the browser's "view page source" - I can see the XML response here.

Testing...
Good Response

*** Response from Server ***

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><employee><empAddress>San Antonio TX</empAddress><empId>1111</empId><empName>EmployeeOne</empName></employee>



That's because that's not XML.
You have a whole load of spurious text at the front of it.

The reason that text/xml works is because it's far more lenient.
You shouldn't be relying on that, though.
If you should be proper XML, then it should be application/xml, and if it's rejected then your XML is incorrect and should be fixed.



I removed all the front text and changed "text/xml" back to "application/xml". It is working fine now. Thank you @Dave Tolls. But I have a new issue here. I protected my RESTFul service that is being called form my servlet in web.xml through <security-constraint>. But I cannot call this service from servlet when it is protected through <security-constraint>, when I comment that out, it is no mpre protected and I can call the service from the client. Please suggest.

SERVICE WAR
ent_securityprefs_empService
-src
-com.xxxx.channel.employee.service
-Employee.java
-Employees.java
-EmployeeService.java
-com.xxxx.channel.employee.service.bean
-EmployeeBean.java
-WebContent
-WEB-INF
-beans.xml
-jboss-web.xml
-web.xml
-hello.jsp

Employee.java

Employees.java

EmployeeService.java

EmployeeBean.java
@ApplicationScoped
public class EmployeeBean implements Serializable{

private TreeMap employeeMap;

public EmployeeBean(){
employeeMap = new TreeMap();
}

public TreeMap getEmployeeMap() {
return employeeMap;
}

public void setEmployeeMap(TreeMap employeeMap) {
this.employeeMap = employeeMap;
}

}
beans.xml

jboss-web.xml

web.xml

hello.jsp


CLIENT WAR
ent_securityprefs_emp
-src
-EmployeeServlet.java
-WebContent
-WEB-INF
-jboss-deployment-structure.xml
-jboss-web.xml
-web.xml
-index.jsp

EmployeeServlet.java

jboss-deployment-structure.xml

Note: This module has all the required dependencies in it.
jboss-web.xml

web.xml

index.jsp


SERVER CONFIGURATION FILE
standlaone-full.xml
Note: i am only addign security-domain part to avoid confusion. This is the security domain to which my service and client are configured to.
 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

swathi bairu wrote:

Dave Tolls wrote:

swathi bairu wrote:When I open the browser's "view page source" - I can see the XML response here.

Testing...
Good Response

*** Response from Server ***

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><employee><empAddress>San Antonio TX</empAddress><empId>1111</empId><empName>EmployeeOne</empName></employee>



That's because that's not XML.
You have a whole load of spurious text at the front of it.

The reason that text/xml works is because it's far more lenient.
You shouldn't be relying on that, though.
If you should be proper XML, then it should be application/xml, and if it's rejected then your XML is incorrect and should be fixed.



I removed all the front text and changed "text/xml" back to "application/xml". It is working fine now. Thank you @Dave Tolls. But I have a new issue here. I protected my RESTFul service that is being called form my servlet in web.xml through <security-constraint>. But I cannot call this service from servlet when it is protected through <security-constraint>, when I comment that out, it is no mpre protected and I can call the service from the client. Please suggest.

SERVICE WAR
ent_securityprefs_empService
-src
-com.xxxx.channel.employee.service
-Employee.java
-Employees.java
-EmployeeService.java
-com.xxxx.channel.employee.service.bean
-EmployeeBean.java
-WebContent
-WEB-INF
-beans.xml
-jboss-web.xml
-web.xml
-hello.jsp

Employee.java

Employees.java

EmployeeService.java

EmployeeBean.java
@ApplicationScoped
public class EmployeeBean implements Serializable{

private TreeMap employeeMap;

public EmployeeBean(){
employeeMap = new TreeMap();
}

public TreeMap getEmployeeMap() {
return employeeMap;
}

public void setEmployeeMap(TreeMap employeeMap) {
this.employeeMap = employeeMap;
}

}
beans.xml

jboss-web.xml

web.xml

hello.jsp


CLIENT WAR
ent_securityprefs_emp
-src
-EmployeeServlet.java
-WebContent
-WEB-INF
-jboss-deployment-structure.xml
-jboss-web.xml
-web.xml
-index.jsp

EmployeeServlet.java

jboss-deployment-structure.xml

Note: This module has all the required dependencies in it.
jboss-web.xml

web.xml

index.jsp


SERVER CONFIGURATION FILE
standlaone-full.xml
Note: i am only addign security-domain part to avoid confusion. This is the security domain to which my service and client are configured to.



I felt like it is altogether a new topic. So I posted it as a "new topic" in https://coderanch.com/t/657583/JBoss/Calling-Secured-RESTEasy-service-servlet#3048191
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
OK, so I've closed this one.
 
    Bookmark Topic Watch Topic
  • New Topic