title: Sending XML message to JMS queue
body: <p>am working with a project in which I am trying to call a Servlet that will push XML message to a JMS queue running in my local machine, using java code. You can find the actual Servlet code below, which actually sends the message to the queue.</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  import javax.jms.JMSException;
  import javax.jms.MessageProducer;
  import javax.jms.ObjectMessage;
  import javax.jms.Queue;
  import javax.jms.QueueConnection;
  import javax.jms.QueueConnectionFactory;
  import javax.jms.QueueSender;
  import javax.jms.QueueSession;
  import javax.jms.TextMessage;
  import javax.naming.Context;
  import javax.naming.InitialContext;
  import javax.naming.NamingException;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  
  public class MessageSender
    extends HttpServlet
  {
    private static final long serialVersionUID = 1L;
    static PrintWriter out;
    public static final String CNN_FACTORY = "jms/alert/connectionFactory";
    public static final String QUEUE_NAME = "jms/alert/amlScreenQueue";
    public static String currentQueueName;
    public static String JMSConnectionFactory;
    private QueueConnection qcon;
    private QueueSession qsession;
    private static QueueSender qsender;
    private Queue queue;
    private static ObjectMessage om;
    private static MessageProducer messageProducer;
  
    public MessageSender() {}
  
    public void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
    {
      try
      {
        String JMSQueueName = request.getParameter("JMSQueueName");
        currentQueueName = JMSQueueName;
  
        JMSConnectionFactory = request.getParameter("JMSConnectionFactory");
        out = response.getWriter();
        InitialContext ic = getInitialContext();
        init(ic, JMSQueueName);
        doPost(request, response);
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
  
    public void init(Context ctx, String queueName)
      throws NamingException, JMSException
    {
      QueueConnectionFactory qconFactory = (QueueConnectionFactory)ctx.lookup(JMSConnectionFactory);
      this.qcon = qconFactory.createQueueConnection();
      this.qsession = this.qcon.createQueueSession(false, 1);
      queue = ((Queue)ctx.lookup(queueName));
  
      QueueConnection qcon = qconFactory.createQueueConnection();
      qcon.start();
      QueueSession qsession = qcon.createQueueSession(false, 1);
      qsender = qsession.createSender(queue);
    }
  
  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
    {
      String textMessage = request.getParameter("textMessage");
  
      try
      {
        TextMessage msg = qsession.createTextMessage(textMessage);
        msg.setStringProperty("JMSXGroupID", "1");
        msg.setIntProperty("JMSXGroupSeq", 1);
        msg.setBooleanProperty("JMS_IBM_Last_Msg_In_Group", false);
        qsender.send(msg);
        out.println("Message sent to queue " + currentQueueName);
        out.println("");
        out.println(msg.getText());
      }
      catch (JMSException e)
      {
        e.printStackTrace();
        out.println(e.getErrorCode());
        try
        {
          qsender.close();
          qcon.close();
        }
        catch (JMSException e) {
          e.printStackTrace();
        }
      }
      finally
      {
        try
        {
          qsender.close();
          qcon.close();
        }
        catch (JMSException e) {
          e.printStackTrace();
        }
      }
    }
  
    private static InitialContext getInitialContext() throws NamingException
    {
      return new InitialContext();
    }
  }
  </code></pre>
  
  <p>This is the java code that I am using to call the servlet,</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URISyntaxException;
  import java.net.URL;
  import java.net.URLConnection;
  import java.nio.file.Files;
  import java.nio.file.Paths;
  import java.util.List;
  import java.util.Map.Entry;
  
  public class SendMessage {
  
      public static void main(String[] args) throws URISyntaxException{
          String line;
          StringBuffer buffer = new StringBuffer();
          String data = null;
  
          try 
          { 
              String filePath = "data/payload.xml";
              File dataFile = new File(filePath);
              if(dataFile.exists()){
  
                  data = new String(Files.readAllBytes(Paths.get(filePath)));                
  
              }else{
                  System.out.println("Not Available");
              }
  
  
  
              String localUrl = "http://localhost:8080/SimulationTool/MessageSender";
              String params = "JMSQueueName=jms/alert/amlScreenQueue&amp;"
                      + "JMSConnectionFactory=jms/alert/connectionFactory&amp;"
                      + "textMessage=" + data;
  
              System.out.println("PARAMS:::" + params);
  
              URL url = new URL(localUrl);
  
              System.out.println("URL FOUND:::" + url.toURI());
  
              URLConnection conn = url.openConnection();
              conn.setDoOutput(true);
              conn.connect();
  
              System.out.println("Response Code:::" + ((HttpURLConnection)conn).getResponseCode());
  
              BufferedWriter out = new BufferedWriter( new OutputStreamWriter( conn.getOutputStream() ));
              out.write(params);
              out.flush();
              out.close();
  
              // Response code
              System.out.println("Response Code:::" + ((HttpURLConnection)conn).getResponseCode());
  
              // Response values
              for (Entry&lt;String, List&lt;String&gt;&gt; header : ((HttpURLConnection)conn).getHeaderFields().entrySet()) {
                  System.out.println(header.getKey() + "=" + header.getValue());
              }
  
  
              BufferedReader in = new BufferedReader( new InputStreamReader( conn.getInputStream() ) );                
              String response;
              while ( (response = in.readLine()) != null ) {
                  System.out.println( response );
              }
              in.close();
  
          }
          catch ( MalformedURLException ex ) {
              ex.printStackTrace();
          }
          catch ( IOException ex ) {
              ex.printStackTrace();
          } 
      }
  
  }
  </code></pre>
  
  <p>The problem is its failing with below exception,</p>
  
  <pre><code>Response Code:::200
      java.net.ProtocolException: Cannot write output after reading input.
          at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(HttpURLConnection.java:1312)
          at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1291)
          at servlettests.SendMessage.main(SendMessage.java:63)
  </code></pre>
  
  <p>Its shows response code as 200, so the connection was successful but it fails while getting the outputstream, which is being created to send message to the queue. Pls help me on resolving this issue.</p>