Can anyone please help me with my sorting Array...my teacher asked me to do a code for sorting array using html and servlet. I don't know how to start this program since I am still new in java...please help me...if you can provide example code then that would me appreciated...

What efforts have you put in so far? Please post any coding you have done using code tags.

This is my html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<p>Sorting arrays</p>
<form action="Sorting" method=get>
<p>1 <input type=text name=word></p>
<input type=submit name=sort value=Submit>
</form>
</body>
</html>

This is my java servlet

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

public class Sorting extends HttpServlet
{
    public void doGet(HttpServletRequest request,
         HttpServletResponse response)
            throws IOException, ServletException
    {
        
        int word = Integer.parseInt(request.getParameter("word"));
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Sorting Servlet</title>");
        out.println("</head>");                  
        out.println("<body>");
        out.println("<p>The sorted words are: "+ word);
        out.println("</body>");
        out.println("</html>");
    }
    
    public void doPost(HttpServletRequest request,
       HttpServletResponse response)
             throws IOException, ServletException
    {
      doGet(request, response);
    }

    double word;
    print(word);       //print unsorted array
    Arrays.sort(word);   //sort the array
    print(word);        //print sorted array
    

}

I need help...It doesn't compile due to unknown error...help me...

> double word;
> print(word); //print unsorted array
> Arrays.sort(word); //sort the array
> print(word); //print sorted array

What are these things doing outside a method? Why is 'word' of type double and why is it declared as member variable of the servlet class which in itself lends to poor design since it would be shared by all your clients? What kind of an array do you want to sort -- an array of numbers or an array of strings? Nowhere in your markup (html file) do you provide the provision for accepting multiple values. You either need to provide such a provision or ask the user to enter values separated by some delimiter, for instance, a comma.

Assuming that you are trying to sort an array of strings provided by the user as comma separated values, you need to do something like this:

public class Sorting extends HttpServlet
{
    public void doGet(HttpServletRequest request,
         HttpServletResponse response)
            throws IOException, ServletException
    {        
        String word = request.getParameter("word");
        word = process(word);
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Sorting Servlet</title>");
        out.println("</head>");                  
        out.println("<body>");
        out.println(word);
        out.println("</body>");
        out.println("</html>");
    }
    
    public void doPost(HttpServletRequest request,
       HttpServletResponse response)
             throws IOException, ServletException
    {
      doGet(request, response);
    }

    public String process(String word) {
        StringBuilder buf = new StringBuilder(512);
        if(word != null && word.trim().length() != 0) {
            String arr[] = word.replaceAll("\\s+", "").split(",");
            Arrays.sort(arr);
            for(String str : arr) {
                buf.append(str).append("<br>");
            }
        }
        else {
            buf.append("<br>").append("No input supplied").append("<br>");
        }
        return(buf.toString());
    }
}

Having said all this, it's not a good idea to push the responsibility of presenting data to a servlet, use servlets as application logic components and use a JSP / JSF page for presentation. Read the Sun's J2EE documentation for a comprehensive tutorial on Servlets / JSP.

This question has already been answered. Start a new discussion instead.