login.java:
代碼如下:
package com.ncu;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class login extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res){
try{
//res.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=gbk");
PrintWriter pw=res.getWriter();
//返回登錄界面
pw.print("<html>");
pw.print("<head>");
pw.print("<title>登錄界面</title></head>");
pw.print("<body>");
pw.print("<h1>用戶登錄界面</h1>");
pw.print("<form action='loginpro' method=post>");
pw.print("用戶名:<input type=text name=userName><br>");
pw.print("密碼:<input type=password name=Pwd><br>");
pw.print("<input type=submit value=submit>");
pw.print("</body></html>");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
LoginPro.java:
代碼如下:
package com.ncu;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginPro extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res){
try{
String user=req.getParameter("userName");
String pwd=req.getParameter("Pwd");
if(user==null){
user="";
}
if(pwd==null)
{
pwd="";
}
if(user.equals("nicolas")&&pwd.equals("nicolas")){
//用戶和密碼合法
//res.sendRedirect("wel");
// res.setCharacterEncoding("gbk");
// PrintWriter pw=res.getWriter();
// pw.print("恭喜你,登錄成功!!");
res.sendRedirect("welcome");
}
else{
//不合法進行跳轉
res.sendRedirect("login");//跳轉到登錄界面
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
Welcome.java:
代碼如下:
package com.ncu;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Welcome extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res){
try{
res.setCharacterEncoding("gbk");
PrintWriter pw=res.getWriter();
pw.print("welcome,您好");
}catch(Exception e)
{
e.printStackTrace();
}
}
}
web.xml
代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<!-- 給你的servlet取名,任意的 -->
<servlet-name>hello</servlet-name>
<!-- 指明servlet的路徑(包名+類名 -->
<servlet-class>com.ncu.Hello</servlet-class>
</servlet>
<servlet-mapping>
<!-- 這個servlet名必須和映射的一致 -->
<servlet-name>hello</servlet-name>
<!-- 這是在浏覽器中輸入的訪問該servlet的url -->
<url-pattern>/nicolas</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>helloservlet</servlet-name>
<servlet-class>com.ncu.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloservlet</servlet-name>
<url-pattern>/helloservlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>helloHttpServlet</servlet-name>
<servlet-class>com.ncu.helloHttpServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloHttpServlet</servlet-name>
<url-pattern>/helloHttpServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.ncu.login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>loginpro</servlet-name>
<servlet-class>com.ncu.LoginPro</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginpro</servlet-name>
<url-pattern>/loginpro</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>com.ncu.Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>