Last active
November 29, 2018 03:41
-
-
Save LongClipeus/a3563955607b5a9a56020e65730ea156 to your computer and use it in GitHub Desktop.
Writting a string from servlet java to javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package servlet; | |
import java.io.IOException; | |
import javax.servlet.RequestDispatcher; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
@WebServlet(name = "DemoServlet", urlPatterns = {"/DemoServlet"}) | |
public class DemoServlet extends HttpServlet { | |
protected void processRequest(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
response.setContentType("text/html;charset=UTF-8"); | |
RequestDispatcher RequetsDispatcherObj = request.getRequestDispatcher("index.jsp"); | |
RequetsDispatcherObj.forward(request, response); | |
} | |
@Override | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
processRequest(request, response); | |
} | |
@Override | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
processRequest(request, response); | |
} | |
@Override | |
public String getServletInfo() { | |
return "Short description"; | |
} | |
public static String sayHello() { | |
return "Hey, I'm robot. And you?"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@page import="servlet.DemoServlet"%> | |
<%@page contentType="text/html" pageEncoding="UTF-8"%> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
<title>Home Page</title> | |
</head> | |
<body> | |
<input type="button" value="Click Me" onclick="loadText()" /> | |
<div id="resultId"> | |
Nội dung ajax sẽ được load ở đây | |
</div> | |
<script language="javascript"> | |
function loadText() { | |
document.getElementById("resultId").innerHTML = String("<%= DemoServlet.sayHello() %>"); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment