好啦\(^o^)/~废话不多说,上代码。
我用的是eclipse。 jdk8.0 tomcat8.0
首先是创建Web工程
左上角 File → New → Dynamic Web Project
然后下一步 记得下面的要修改一下
下一步 记得要选上xml哦
然后下面是项目码完的参照图,如果文件创错记得回来看看
首先是Servlet
这里直接选中src右键→new→Servlet
完了以后直接完成就好(ps:这里是直接包名类名一起创建的。不是好习惯。正常情况是先创建包,再创建类)
这里是Servlet ↓↓↓
package com.etoak.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Servlet
*/
@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Servlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
//request.setCharacterEncoding("UTF-8")的作用是设置对客户端请求进行重新编码的编码。
response.setContentType("text/html;charset:UTF-8");
//response.setContentType("UTF-8")的作用是指定对服务器响应进行重新编码的编码。
String name = request.getParameter("name");
//获取index页面的用户名
String password = request.getParameter("pwd");
//获取index页面的密码
if (name.equals("penny") && password.equals("123456")) {
//判定用户名和密码 这里写死了
response.sendRedirect("success.jsp");
//跳转至登录成功页面
} else {
response.sendRedirect("shibai.jsp");
//跳转至登录失败页面
}
}
}
jsp页面直接在WebContent下直接右键创建
选中WebContent→New→Jsp File
这里是index.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/Servlet" method="post">
用户姓名:<input type="text" name="name" />
<br>
用户密码:<input type="password" name="pwd" />
<br>
保存密码:<input type="radio" name="life" value="31536000" />
<br>
<input type="submit" value="提交" />
<input type="reset" value="销毁" />
</form>
</body>
</html>
这里是success.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登陆成功</title>
</head>
<body>
<h1>登陆成功!!!</h1>
<input type="button" name="Submit" onclick="javascript:history.back(-1);" value="返回再试一次?">
</body>
</html>
这里是shibai.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录失败!!!
<input type="button" name="Submit" onclick="javascript:history.back(-1);" value="重新登录">
</body>
</html>
到此这个小程序就算完工了
然后就是tomcat运行让项目跑起来!!ヾ(◍°∇°◍)ノ゙
选中右键 点击 Add and Remove
找到刚刚创建的项目
然后就可以启动啦~~~
记得看看有木有报错~~
在游览器输入 http://localhost:8080/Jsp_Servlet/index.jsp
成功界面(用户名和密码输入正确)
登录失败(用户名密码输入错误)
下面附赠 页面报错指南
Servlet出错攻略大全!
修改html jsp等页面不需要关闭tomcat
修改servlet或者xml必须关闭tomcat,修改好
之后重新打开tomcat
1:404根据开发者提供的路径没有找到资源(ps:我们的这个小程序里Servlet已经在Servlet类里使用注解的方式帮我们注册了servelt了,这时候就不需要我们在web.xml里面手动注册了。)
A:404后面没有跟随路径
tomcat开启失败,开启失败原因
1):tomcat文件缺失
2):web.xml解析失败,无法开启tomcat
3):已经开启了一个tomcat
B:404后面存在路径
根据此处打印的路径没有找到资源
例如action提交之后与web.xml文件中的
url-pattern节点不对应
2:500
java编译失败,或者通过web.xml中的
servlet-class节点无法找到servlet实例
can not found class *** Exception
3:405
get~~>doPost
post~~>doGet