package com.fyt.org;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public LoginServlet() {
super();
}
public void destroy() {
super.destroy();
}
//使用Get方式向服务器提交数据
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
//使用Post方式向服务器提交数据
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取从浏览器中发送过来的用户名
String username = request.getParameter("username");
//获取从客户端发送过来的密码
String password = request.getParameter("password");
//使用iso8859-1编码将username转换成字节数组
//再使用utf-8把字节数组转换成字符串
username = new String(username.getBytes("iso8859-1"), "utf-8");
//在控制台中打印用户名和密码
System.out.println("username=" + username);
System.out.println("password=" + password);
//获得一个输出流
OutputStream os = response.getOutputStream();
//如果用户名和密码都输入正确
if("小志".equals(username) && "123".equals(password)) {
//将字符发送至浏览器中
os.write("登录成功".getBytes("utf-8"));
}
else {
//将字符串发送到浏览器中
os.write("登录失败".getBytes("utf-8"));
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="servlet/LoginServlet" method="post"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="提交"> </form> </body> </html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有