通過servlet,useBean動作製作登陸頁面
阿新 • • 發佈:2018-12-19
第一步:先寫一個登陸頁面,將登陸資訊提交到LoginServlet。
<h1>登陸頁面</h1> <hr> <form action="LoginServlet" method="post"> <table> <tr> <td>使用者名稱:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>密碼:</td> <td><input type="password" name="password"></td> </tr> <tr> <td>愛好:</td> <td><input type="checkbox" name="interest" value="籃球">籃球 <input type="checkbox" name="interest" value="足球">足球 <input type="checkbox" name="interest" value="乒乓球">乒乓球 </td> </tr> <tr> <td><input type="submit" value="提交"></td> </tr> </table> </form>
第二步:寫一個控制處理提交資訊的servlet的類,並將處理的資訊傳入user物件中儲存,同時從request中獲得本次操作的session,並將user中儲存的資訊存入其中。然後將request轉發給doLogin.jsp檔案。
package com.wh.controller; 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; import com.wh.entity.User; @SuppressWarnings("serial") public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); String password = request.getParameter("password"); String[] interests = request.getParameterValues("interest"); User user = new User(username, password, interests); HttpSession session = request.getSession(); session.setAttribute("user", user); request.getRequestDispatcher("/doLogin.jsp").forward(request, response); } }
第三步:幫LoginServlet配置web.xml檔案。
<servlet> <servlet-name>loginServlet</servlet-name> <servlet-class>com.wh.controller.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>loginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping>
第五步:寫一個user的avaBean。
ublic class User {
private String username;
private String password;
private String[] interests;
public User(String username, String password, String[] interests) {
super();
this.username = username;
this.password = password;
this.interests = interests;
}
public User() {
super();
}
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;
}
public String[] getInterests() {
return interests;
}
public void setInterests(String[] interests) {
this.interests = interests;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + ", interests=" + Arrays.toString(interests)
+ "]";
}
}
第六步:通過useBean動作獲得存放在javaBean中的username和password並輸出,通過session獲得Internets的資訊並遍歷輸出。
<h1>處理登陸頁面</h1>
<hr>
<jsp:useBean id="user" class="com.wh.entity.User" scope="session" />
使用者名稱:<jsp:getProperty property="username" name="user" />
<br> 密碼:<jsp:getProperty property="password" name="user" />
<br> 愛好:<%
User u = (User) session.getAttribute("user");
String[] interests = u.getInterests();
for (String s : interests) {
out.print(s + " ");
}
%>
總結:編寫程式碼的過程中肯定是會遇到bug的,要在實踐中慢慢摸索自己的解決問題的辦法。我的經驗是遇到bug先看報錯資訊,找到對應的地方進行修改,修改後問題沒有解決的話,試著加入一些其他的除錯程式,來精確定位bug是出在什麼地方,找到了出問題的地方,就等於成功了一半。