Answer the question
In order to leave comments, you need to log in
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>
@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);
}
public class MyClass {
public void method1(){
System.out.println("Helo World1!");
}
}
Answer the question
In order to leave comments, you need to log in
public class MyClass {
public static void main(String[] args) {
method1();
}
public static void method1(){
System.out.println("Helo World1!");
}
}
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);
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 questionAsk a Question
731 491 924 answers to any question