JSP環境及其第一個動態網頁
阿新 • • 發佈:2019-02-09
環境
tocmat8.5eclipse
jdk
繫結tomcat伺服器
點選window--->Preference--->Server--->Runtime Environment--->Add--->選擇Apach Tomat v8.5--->Next--->Finish。第一個動態網頁
1、建立一個web專案 File--->New--->Dynamic Web Project--->輸入專案名--->Target runtime改為8.5--->將Dynamic web module version改為3.0--->Next--->Generate web.xml de...選擇--->Next--->Finish這時在WebContent/WEB-INF/下已經自動生成web.xml檔案。3、此時WebContent/WEB-INF/下的web.xml檔案也已經自動配置好了。 4、新增其他Java類package servlets; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import bean.*; /** * Servlet implementation class LoginServlet */ @WebServlet("/login") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()).append("<br />"); String username = request.getParameter("username"); String userpass = request.getParameter("userpass"); response.getWriter().append(username).append(request.getContextPath()).append("<br />"); response.getWriter().append(userpass).append(request.getContextPath()).append("<br />"); UserBean user = new UserBean(); boolean b = user.validate(username, userpass); String sult; String forward; if(b) { sult = "tur"; }else { sult = "fal"; } response.getWriter().append(sult).append(request.getContextPath()).append("<br />"); if(b) { HttpSession session = request.getSession(true); session.setAttribute("userid", username); forward = "success.jsp"; }else{ forward = "failure.jsp"; } RequestDispatcher dispatcher = request.getRequestDispatcher(forward); dispatcher.forward(request, response);/**/ } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
選擇Java Resource--->src--->New--->Class--->包名:bean--->類名:UserBean--->finish。 程式碼例項:
5、新增jsp程式碼(3個檔案) 選擇WebContent--->New--->JSP file--->檔名:login.jsp。 login.jsp程式碼:package bean; public class UserBean { private String username; private String userpass; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; } public boolean validate(String username, String userpass) { if(username.equals("zhangsan") && userpass.equals("wangwu")) return true; else return false; } }
<%@ page contentType="text/html; charset=UTF-8"%>
<script language="JavaScript">
function isValidate(form)
{
username = form.username.value;
userpass = form.userpass.value;
if(!minLength(username, 6))
{
alert("小於6!");
form.username.focus();
return false;
}
if(!maxLength(username, 8))
{
alert("大於8!");
form.username.focus();
return false;
}
if(!minLength(userpass, 6))
{
alert("口令6!");
form.userpass.focus();
return false;
}
if(!maxLength(userpass, 8))
{
alert("口令8!");
form.userpass.focus();
return false;
}
return true;
}
function minLength(str, length)
{
if(str.length>=length)
return true;
else
return false;
}
function maxLength(str, length)
{
if(str.length <= length)
return true;
else
return false;
}
</script>
<html>
<head>
<title>使用者登入</title>
</head>
<body>
<h2>使用者登入</h2>
<form name="form1" action="login" method="post" onsubmit="return isValidate(form1)">
使用者名稱:<input type="text" name="username"><br />
口令:<input type="password" name="userpass"><br />
<input type="reset" value="重置">
<input type="submit" value="提交"><br />
</form>
</body>
</html>
success.jsp程式碼:
<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>登入成功</title>
</head>
<body>
<h2>${sessionScope.userid}hello!</h2>
</body>
</html>
failure.jsp程式碼:
<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>登入失敗</title>
</head>
<body>
<h2>使用者名稱或者口令不正確,請<a href="login.jsp">重新登入!</a></h2>
</body>
</html>
6、專案部署到tomcat伺服器中
點選window--->show view--->servers如下所示
如果已有專案,將專案移除後,右鍵點選clean,再雙擊,可以看到如下頁面:
選擇第二個,use Tomcat installation(takes control of Tomcat installation)再點選Deploy path右邊的Browse,選擇tomcat目錄下的web應用目錄,Ctrl+s儲存。 7、啟動 run--->run as--->run on server;
Next--->選擇專案--->Add>--->Finish。 8、瀏覽器 輸入http://localhost:8080/test/login.jsp,使用者名稱:zhangsan,口令:wangwu
中文亂碼
response.setCharacterEncoding("UTF-8");其他問題
Several ports (8005, 8080, 8009) required by Tomcat v8.5 Server at localhost are already in use. The server may already be running in another process, or a system process may be using the port. To start this server you will need to stop the other process or change the port number(s).直接在工作管理員中關閉tomcat。