Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

JSP Works, But Servlet Doesn't???

stricksstricks Member Posts: 33
Ok, basically I'm wanting to store into a mutidimensional array and then dispay it.

In my jsp I can do it. Here's the code:

*************************************************************
<%

String[][] questions = new String[10][10];

questions[0][0] = "waht";
out.println(questions[0][0]);
%>
*************************************************************

That works, but then I have basically the exact same code in a servlet and it won't work. What is the problem???

Servlet Code:
******************************************************************

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

public class ArrayTesting extends HttpServlet{

public String[][] questions = new String[10][10];


public void service(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException{

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Questions String :
");

questions[0][0] = "waht";
out.println(questions[0][0]);


}
}
********************************************************

Thank you.

Comments

  • lykichlykich Member Posts: 51
    [b][red]This message was edited by lykich at 2005-6-3 7:56:32[/red][/b][hr]
    in the servlet you must have doGet() && doPost() methods
    [code]
    protected void doGet
    (HttpServletRequest request, HttpServletResponse response){
    doPost(request,response);
    }

    protected void doPost
    (HttpServletRequest request, HttpServletResponse response){
    service(request,response);
    }
    [/code]


  • stricksstricks Member Posts: 33
    [b][red]This message was edited by stricks at 2005-6-3 8:16:13[/red][/b][hr]

  • stricksstricks Member Posts: 33
    I've only been doing Servlets for a few weeks now, but I've never used those methods and I've had them working. My over all servlet will work, but it just doesn't work when I try to do the Multidimension Array.

    Do you have an example using those methods that would help me out with my array problem?

    Thanks for your time.

    Brian
  • lykichlykich Member Posts: 51
    [code]
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

    public class ArrayTesting extends HttpServlet{

    public String[][] questions = new String[10][10];


    public void doStuff(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("Questions String :
    ");
    try{
    questions[0][0] = "Working waht";
    out.println(questions[0][0]);
    out.close();
    }
    catch(Exception e){
    out.println(e.getMessage());
    }
    finally{
    out.close();
    }

    }
    protected void doGet
    (HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
    doPost(request,response);
    }

    protected void doPost
    (HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException{
    doStuff(request,response);
    }

    }


    [/code]

  • stricksstricks Member Posts: 33
    Thanks, that help. I left out the try/catch blocks which is what fixed my problem.

    Thanks again,

    stricks
Sign In or Register to comment.