Answer the question
In order to leave comments, you need to log in
Error when outputting "Hello World!" part 2?
Guys, hello!
I ask you to help with the output of the text "Helo World" from the class method on clicking the button. It doesn't give an error, just nothing happens.
index.jsp:
<form action="${pageContext.request.contextPath}/myservlet" method="post">
<button type="submit" name="button" value="button1">Button 1</button>
<button type="submit" name="button" value="button2">Button 2</button>
<button type="submit" name="button" value="button3">Button 3</button>
</form>
@WebServlet("/myservlet")
public class MyServlet extends HttpServlet {
@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 if ("button2".equals(button)) {
myClass.method2();
} else if ("button3".equals(button)) {
myClass.method3();
} else {
// ???
}
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
public class MyClass {
public void method1(){
System.out.println("Helo World1!");
}
public void method2(){
System.out.println("Helo World2!");
}
public void method3(){
System.out.println("Helo World3!");
}
}
Answer the question
In order to leave comments, you need to log in
According to java, I can say that, in addition to the implementation of the methods themselves, we need a starting point.
After declaring the class, you need to add the public static void main method, inside which to call all the other methods, which, in turn, must be made static, since an instance of the class is not created.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question