V
V
Viktoria Smirnova2017-10-05 07:42:58
Java
Viktoria Smirnova, 2017-10-05 07:42:58

What is wrong with the output "Hello World!" part 3?

Guys, what's wrong with the output of "Helo World"?

in the index:

<form action="${pageContext.request.contextPath}/myservlet" method="post">
    <button type="submit" name="button" value="button1">Button 1</button>
</form>


in servlet:
@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MyClass myClass = new MyClass();
        String button = request.getParameter("button");

        if ("button1".equals(button)) {
            myClass.method1();
        } else {
            // ???
        }

        request.getRequestDispatcher("index.jsp").forward(request, response);
    }


in class:

public class MyClass {
public void method1(){
System.out.println("Helo World1!");
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Manul, 2017-10-05
Clawed @Manul81

public class MyClass {
public static void main(String[] args) {
method1();
}
public static void method1(){
System.out.println("Helo World1!");
}
}

A
Alexey Cheremisin, 2017-10-10
@leahch

Well, if you want to output something in html, then use
it as - described here - www.oracle.com/technetwork/java/servlet-142430.html
porridge!)
And if you want to transfer something to index.jsp, then

String message = "HI Welcome";
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
request.setAttribute("msg",message);
rd.forward(request, response);

Well, completely probably (did not check):
public class MyClass {
  public String method1(){
   return("Helo World1!");
  }
}

protected void doPost(...) {
...
String  mymsg = myClass.method1();
...
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
request.setAttribute("msg", mymsg);
rd.forward(request, response);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question