servlet常用调用方式实例代码
<html>
<head>
<title>TestHtml.html</title>
<meta name="keywords" content="keyword1,keyword2,keyword3">
<meta name="description" content="this is my page">
<meta name="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
function test()
{
var f = document.forms[0];
f.action="servlet/helloworod";
f.submit();
}
</script>
</head>
<body>
<form name="f1" id="f1" action="servlet/helloworod" method="post">
<table>
<tr>
<td>Login:</td>
<td><input type="text" name="login" id="login"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" id="password"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="post submit"></td>
<td colspan="2"><input type="button" value="script submit" onclick="test();"></td>
</tr>
<tr>
<td colspan="2"><a href="servlet/helloworod">Link...</a></td>
</tr>
</table>
</form>
</body>
</html>
测试servlet代码
package servletStudent;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class helloworod extends HttpServlet {
/**
* Constructor of the object.
*/
public helloworod() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String url = request.getRequestURL().toString();
System.out.println("=============================");
System.out.println(url);
PrintWriter out = response.getWriter();
out.println("=======get========");
out.println("Hello world!!! ");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String url = request.getRequestURL().toString();
System.out.println("=============================");
System.out.println(url);
PrintWriter out = response.getWriter();
out.println("=======post========");
out.println("Hello world!!! ");
}
}
网友评论