使用Servlet可以读取用户输入的信息并跳转页面显示出来,代码如下:
LoginServlet代码:
package com.djx.sevlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsServer;
import sun.security.krb5.internal.SeqNumber;
public class LoginServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
/* @Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
String username=req.getParameter("uname");
String upassword=req.getParameter("upwd");
System.out.println("用户名:"+username);
System.out.println("密码:"+upassword);
//super.service(req, resp);
}*/
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("进入doGet方法");
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("进入doPost方法");
String username=req.getParameter("uname");
String upassword=req.getParameter("upwd");
System.out.println("用户名:"+username);
System.out.println("密码:"+upassword);
String forward=null;
if (username=="djx" && upassword=="123") {
//请求重定向
// resp.sendRedirect(req.getContextPath()+"/15/success.jsp");
forward="/15/success.jsp";
RequestDispatcher rd=req.getRequestDispatcher(forward);
rd.forward(req, resp);
}else{
//resp.sendRedirect(req.getContextPath()+"/15/error.jsp");
forward="/15/success.jsp";
RequestDispatcher rd=req.getRequestDispatcher(forward);
rd.forward(req, resp);
}
//super.doPost(req, resp);
}
}
login.jsp代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/LoginServlet" method="post">
用户名:<input type="text" name="uname"><br/>
密 码:<input type="password" name="upwd"><br/>
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
success.js代码如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录成功页面提示</title>
</head>
<body>
登陆成功。<br/>
您提交的信息为:<br/>
用户名:<%=request.getParameter("uname") %><br/>
密码:<%=request.getParameter("upwd") %><br/>
<a href="login.jsp">返回登录页面</a>
</body>
</html>
error.jsp代码:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>登录失败提示</title>
</head>
<body>
登陆失败。<br/>
您提交的信息为:<br/>
用户名:<%=request.getParameter("uname") %><br/>
密码:<%=request.getParameter("upwd") %><br/>
<a href="login.jsp">返回登录页面</a>
</body>
</html>
在xml文件加入如下代码:
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.djx.sevlet.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>