Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My first servlet

XML
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet
{
  public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
  {
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
    String sqanda = "1.What does TCP stands for?/Transaction Control Protocol/Transmission Control Protocol/Transport Control Protocol/Terminal Control Protocol";
    String q,a,a1,a2,a3,a4;
    StringTokenizer st = new StringTokenizer(sqanda,"/");
    while(st.hasMoreTokens())
    {
    q=st.nextToken();
    a1=st.nextToken();
    a2=st.nextToken();
    a3=st.nextToken();
    a4=st.nextToken();
    pw.println("<html>");
    pw.println("<head>");
    pw.println("<title>Test</title>");
    pw.println("</head>");
    pw.println("<body>");
    pw.println("<h2>"+q+"</h2>");
    pw.println("<form name=\"test1\" action=\"./aservlet\" method=\"POST\">");
    pw.println("<select size=\"4\" name=\"ansselect\">");
    pw.println("<option value="+a1+">"+a1+"</option>");
    pw.println("<option value="+a2+">"+a2+"</option>");
    pw.println("<option value="+a3+">"+a3+"</option>");
    pw.println("<option value="+a4+">"+a4+"</option>");
    pw.println("</select>");
    pw.println("<p></p>");
    pw.println("<input type=\"submit\"    value=\"submit\">");
    pw.println("</form>");
    pw.println("</body>");
    pw.println("</html>");
 }
 catch(JessException e)
 {
 System.out.println(e);
 }
 }
}


Second Servlet

C#
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import jess.*;

public class AssessmentServlet extends HttpServlet
{
  public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
  {
  response.setContentType("text/html");
  PrintWriter pw = response.getWriter();
  String[] sanswer;
  sanswer = request.getParameterValues("ansselect");
  pw.println(sanswer[0]);
  }
}


when executing by selecting first answer "Transaction Control Protocol" im getting the output as "Transaction" but i want output as "Transaction Control Protocol". How can i do that?
Posted

1 solution

I've not done any Java for years, but I can see what the problem is. When you are printing out your option values, you are not quoting them. The resulting HTML will look like this:
HTML
<option value=Transaction Control Protocol>Transaction Control Protocol</option>


As you can probably tell from the syntax highlighting, this means that your value is "Transaction" and "Control" and "Protocol" end up as meaningless attributes of the option.

Try your println lines like this instead:
Java
pw.println("<option value=\""+a1+"\">"+a1+"</option>");
 
Share this answer
 
Comments
naveen712 5-May-12 9:06am    
Its working.. Thank you so much...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900