一、Servlet使用
1、新建servlet包
2、新建FirstServlet类(new-Servlet),父类为HttpServlet
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
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>");
out.println("<h1>hello</h1>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
3、查看部署文件(Web.xml)
<servlet>
<servlet-name>FirstSevlet</servlet-name>
<servlet-class>servlet.FirstSevlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstSevlet</servlet-name>
<url-pattern>/user/FirstSevlet</url-pattern>
</servlet-mapping>
4、地址栏访问http://localhost:8080/test/user/FirstSevlet
页面显示hello
二、实现用户登录处理
1、登陆页内容
<FORM METHOD=POST ACTION="user/UserServlet?type=login">
<table border="0" style="margin:10px 0 0 10px;">
<tbody><tr>
<td width="50%" valign="top">用户名:</td>
<td><input type="text" name="name" class="txt" size="10"></td>
</tr>
<tr>
<td>密 码:</td>
<td>
<INPUT TYPE="password" NAME="password" size="10" class="txt">
</td>
</tr>
<tr>
<td align="center"><INPUT class="txt" TYPE="submit" value="登录"></td>
<td><INPUT class="txt"TYPE="button" onclick="location.href=‘register.jsp‘" value="注册"></td>
</tr>
</tbody>
</table>
</form>
2、新建UserServlet类(url:user/UserServlet)
doPost方法代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String type=request.getParameter("type");
if("login".equals(type)){
String name = request.getParameter("name");
String pwd = request.getParameter("password");
UserDAO dao=new UserDAO();
User u=dao.login(name, pwd);
if(u!=null){
//request.getRequestDispatcher("index.php").forward(request, response);
request.getSession().setAttribute("user",u);
response.sendRedirect("../index.jsp");
}else{
response.sendRedirect("../index.jsp");
}
}
}
其中UserDAO中login()方法代码
public class UserDAO {
public User login(String name,String pwd)
{
User u=null; //添加
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select * from users where name=? and password=?";
try {
Class.forName(Const.DRIVER);
conn=DriverManager.getConnection(Const.URL, Const.USER, Const.PASSWORD);
ps=conn.prepareStatement(sql);
ps.setString(1, name);
ps.setString(2, pwd);
rs=ps.executeQuery();
if(rs.next()){
u=new User();
u.setUid(rs.getInt("uid"));
u.setName(rs.getString("name"));
u.setPassword(rs.getString("password"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(rs!=null)
rs.close();
if(ps!=null)
ps.close();
if(conn!=null)
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return u;
}
3、web.xml部署
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>servlet.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/user/UserServlet</url-pattern>
</servlet-mapping>