首页 > 代码库 > jsp和servlet 简单登录界面(不连接数据库)
jsp和servlet 简单登录界面(不连接数据库)
1、首先说明一点,虽然很简单但是对于初学者 这个jsp和servlet的链接 我捣鼓了半天才算弄明白
jsp将请求提交到一个url,然后servlet获取也从这个url中获取请求的数据,两者的链接就在那个url
jsp中的代码
文件名:login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>登录</title>
</head>
<body>
<form action="http://localhost:80/loginServlet/LoginServlet" method="post">
用户:<input type="text" name="username" value="http://www.mamicode.com/wangmin"/><br/>
密码:<input type="password" name="password" value="http://www.mamicode.com/wangmin"/><br/>
<input type="submit" value="http://www.mamicode.com/登录" />
</form> aaaa
</body>
</html>
servlet中的代码
文件名:LoginServlet.java
package my.experience.login;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet{
//重写doGet方法
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
//服务器端打印信息
System.out.println("username=" + username);
System.out.println("password=" + password);
//设置编码格式
response.setContentType("text/html;charset=GB18030");
//返回html页面
response.getWriter().println("<html>");
response.getWriter().println("<head>");
response.getWriter().println("<title>登录信息</title>");
response.getWriter().println("</head>");
response.getWriter().println("<body>");
response.getWriter().println("欢迎【" + username + "】用户登录成功!!!");
response.getWriter().println("</body>");
response.getWriter().println("</html>");
}
//重写doPost方法
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException {
doGet(request, response);
}
}
在web.xml中一定要写对 url,要不然找不到
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>MyExperience</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>my.experience.login.LoginServlet</servlet-class>//包名+类名
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/loginServlet/LoginServlet</url-pattern> 对应的jsp中的action
</servlet-mapping>
</web-app>
然后启Tomacat,在浏览器中输入http://localhost:80/login.jsp就ok
本文出自 “autoComplete” 博客,请务必保留此出处http://7129486.blog.51cto.com/7119486/1595806
jsp和servlet 简单登录界面(不连接数据库)