xml文件中的<url-pattern>/servlet/FirstServlet</url-pattern>标签中的值表示我们在浏览器窗口输入http://localhost:/8080/工程名/FirstServlet即可访问到这个servlet文件。
注:http://localhost:/8080 这是tomcat虚拟路径默认的域名和端口号。
首先在WebRoot 文件夹下建立一个html页面 在myeclipse 中右击WebRoot目录选择new -->HTML输入html名称比如index
我们这个servlet项目的目的是进行用户提交数据的验证,判断特定用户和密码组合,都正确则登陆成功
建立一个form进行数据的提交,将数据提交给服务器即servlet程序,进行用户验证。代码:
<!DOCTYPE html>
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="servlet/FirstServlet" method="post" name="login">
<input type="hidden" value="login" name="hidden1">
用户名:<input type="text" name="username"><br><br>
密 码:<input type="password" name="password"><br><br>
<input type="submit" value="登陆">
</form>
</body>
</html>
form action="servlet/FirstServlet" method="post" name="login"//向servlet下的FirstServlet提交数据,采用post提交方式 servlet是工程名
现在我们来写FirstServlet程序因为我们采用post方式提交数据所以我们只需改写FirstServlet中的doPost方法即可
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;chartset=utf-8");//加入charset=utf-8指定浏览器的编码方式防止出现乱码
String userName = request.getParameter("username");// 用户名 通过指定的字段名获取提交的信息
String password = request.getParameter("password");// 密码
PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");if("admin".equals(userName)&&"1234".equals(password)){//用户名为admin并且密码为1234的组合才能登陆成功
out.println("登陆成功");
}else{
out.println("登录失败");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
查看java ee 帮助手册中的
javax.servlet
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
的用法。
这样我们就完成了第一个servlet程序的编写。
注:http://localhost:/8080 这是tomcat虚拟路径默认的域名和端口号。
首先在WebRoot 文件夹下建立一个html页面 在myeclipse 中右击WebRoot目录选择new -->HTML输入html名称比如index
我们这个servlet项目的目的是进行用户提交数据的验证,判断特定用户和密码组合,都正确则登陆成功
建立一个form进行数据的提交,将数据提交给服务器即servlet程序,进行用户验证。代码:
<!DOCTYPE html>
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="servlet/FirstServlet" method="post" name="login">
<input type="hidden" value="login" name="hidden1">
用户名:<input type="text" name="username"><br><br>
密 码:<input type="password" name="password"><br><br>
<input type="submit" value="登陆">
</form>
</body>
</html>
form action="servlet/FirstServlet" method="post" name="login"//向servlet下的FirstServlet提交数据,采用post提交方式 servlet是工程名
现在我们来写FirstServlet程序因为我们采用post方式提交数据所以我们只需改写FirstServlet中的doPost方法即可
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;chartset=utf-8");//加入charset=utf-8指定浏览器的编码方式防止出现乱码
String userName = request.getParameter("username");// 用户名 通过指定的字段名获取提交的信息
String password = request.getParameter("password");// 密码
PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println(" <BODY>");if("admin".equals(userName)&&"1234".equals(password)){//用户名为admin并且密码为1234的组合才能登陆成功
out.println("登陆成功");
}else{
out.println("登录失败");
}
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
查看java ee 帮助手册中的
javax.servlet
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
的用法。
这样我们就完成了第一个servlet程序的编写。