function setFalse(){ if(document.form1.name.value.length!=0 && document.form1.password.value.length!=0){ document.getElementById("s").disabled=false; } }
function setTrue(){ if(document.form1.name.value.length!=0 && document.form1.password.value.length!=0){ document.getElementById("s").disabled=true; } }
</script> <body> <h1 align="center"> 欢迎登陆 </h1> <br> <br> <form id="form1" name="form1" method="post" action="servlet/LoginServlet" onsubmit="return validation(this)"> <table align="center"> <tr> <td> 用户名: </td> <td> <input type="text" name="name" onmouseout="setFalse()"> </td> </tr> <tr> <td> 密 码: </td> <td> <input type="password" name="password" onmouseout="setFalse()"> </td> </tr> <tr> <td> <input type="submit" value="登陆" id="s" disabled> </td> <td> <input type="reset" value="重置" onclick="setTrue()"> </td> </tr> </table> </form> </body> </html> ]客户端servlet: package com.handlewell.control;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 Serv extends HttpServlet { /** * Constructor of the object. */ public Serv() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.print("处理成功!!"); out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here }}
<head>
<base href="<%=basePath%>"> <title>欢迎使用</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<script language="javascript">
function validation(myform){
if(myform.name.value.length==0){
alert("请填写用户名!");
myform.name.focus();
return false;
}
if(myform.password.value.length==0){
alert("请填写密码!");
myform.password.focus();
return false;
}
}
function setFalse(){
if(document.form1.name.value.length!=0 && document.form1.password.value.length!=0){
document.getElementById("s").disabled=false;
}
}
function setTrue(){
if(document.form1.name.value.length!=0 && document.form1.password.value.length!=0){
document.getElementById("s").disabled=true;
}
}
</script> <body>
<h1 align="center">
欢迎登陆
</h1>
<br>
<br>
<form id="form1" name="form1" method="post"
action="servlet/LoginServlet" onsubmit="return validation(this)"> <table align="center"> <tr>
<td>
用户名:
</td>
<td>
<input type="text" name="name" onmouseout="setFalse()">
</td>
</tr>
<tr>
<td>
密 码:
</td>
<td>
<input type="password" name="password" onmouseout="setFalse()">
</td>
</tr>
<tr>
<td>
<input type="submit" value="登陆" id="s" disabled>
</td>
<td>
<input type="reset" value="重置" onclick="setTrue()">
</td>
</tr>
</table> </form>
</body>
</html>
]客户端servlet:
package com.handlewell.control;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 Serv extends HttpServlet { /**
* Constructor of the object.
*/
public Serv() {
super();
} /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doPost(request, response);
} /**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print("处理成功!!");
out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}}
//提交你的表单,假设是a,b <form action="myjsp.jsp" method = "post" >
please enter a:<input type="text" name="a">
please enter b:<input type="text" name="b">
<input type="submit" > </form>提交到web服务器交给servlet处理package com.number.servlet;
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 Number extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
/*做你所要做的东西*/
PrintWriter out = response.getWriter();
out.println("处理成功")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}}
web.xml
<servlet>
<servlet-name>first</servlet-name>
<servlet-class>com.number.servlet.Number</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/first</url-pattern>
</servlet-mapping>