Bookmark Topic Watch Topic
  • New Topic

How to print XML from servlet?

 
swathi bairu
Ranch Hand
Posts: 85
1
  • Mark post as helpful
  • send pies
  • 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.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
Do a View Source at the browser. What do you see?
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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.
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 http://www.coderanch.com/t/657583/JBoss/Calling-Secured-RESTEasy-service-servlet#3048191
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Report post to moderator
OK, so I've closed this one.
 
    Bookmark Topic Watch Topic
  • New Topic