概述
温习一下Servlet的应用:
一、Servlet在web.xml中的配置:
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>Login</servlet-name>
<servlet-class>com.servlet.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/servlet/Login</url-pattern>
</servlet-mapping>
注:<url-pattern>的取值很重要,在表单中要用到。
二、Servlet类如下所示:
package com.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class Login extends HttpServlet {
/**
* Constructor of the object.
*/
public Login() {
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 {
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 {
String nextPage =
"";
HttpSession session = request.getSession();
AccountBean account = new AccountBean();
String userName = request.getParameter("username");
String pwd = request.getParameter("pwd");
account.setUsername(userName);
account.setPassword(pwd);
if(account.getUsername().equals("hongboliu") && account.getPassword().equals("654321")){
System.out.println();
session.setAttribute("account", account);
nextPage = "http://www.google.com.hk";
}else{
nextPage = "http://www.baidu.com";
}
response.sendRedirect(nextPage);
return ;
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
System.out.println("init method");
}
}
三、 AccountBean的设计如下:
package com.servlet;
public class AccountBean {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
四、Form表单内容如下:
<form action="servlet/Login" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="pwd"><br>
<input type="submit" value=" 确 定 ">
</form>
注:form 中action的取值要与web.xml中<url-pattern>的值一致。
五、完成了。
最后
以上就是能干时光为你收集整理的Servlet的复习。的全部内容,希望文章能够帮你解决Servlet的复习。所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复