本文最后更新于14 天前,其中的信息可能已经过时,如有错误请发送邮件到1169063119@qq.com
鸿蒙前端
index.ets
import router from '@ohos.router'
@Entry
@Component
struct PageXXQG {
@State message: string = 'Hello World';
@State cur:number=0
@State cur2:number=0
tab:TabsController=new TabsController()
tab2:TabsController=new TabsController()
scroller:Scroller=new Scroller()
swiperController:SwiperController=new SwiperController()
arr:string[]=['推荐','要闻','思想','发现','北京','综合','直播','快闪','发布']
@Builder tablight(title:string,tar:number,selectedImg:Resource,normalImg:Resource){
Column(){
Image(this.cur==tar?selectedImg:normalImg).size({width:25,height:25})
Text(title).fontColor(this.cur==tar?Color.Red:Color.Gray)
}.onClick(()=>{
this.cur=tar
this.tab.changeIndex(this.cur)
})
}
@Builder TabBu(title:string,tar2:number){
Column(){
Text(title).fontColor(this.cur2==tar2?Color.Red:Color.Gray)
}.onClick(()=>{
this.cur2=tar2
this.tab2.changeIndex(this.cur2)
})
}
build() {
Scroll(this.scroller){
Column(){
//强国通开始
Row(){
Tabs({
barPosition:BarPosition.End,controller:this.tab
}){
TabContent(){
Row(){
Column(){
Row(){
Stack(){
Row(){
Image($r('app.media.xxqg0')).width('18%').margin({bottom:10})
Image($r('app.media.qgt2')).width('30%').margin({bottom:5,left:100,top:5})
Button('我的',{type:ButtonType.Normal}).onClick(()=>{
router.pushUrl({url:'pages/Pagewode'})
}).fontColor('#E8B0A5').margin({left:10}).backgroundColor('#E42417')
}.backgroundColor('#E42417').width('100%')
}
}
Stack(){
TextInput({placeholder:'搜索'}).padding({left:36}).height(42).margin({top:10,bottom:5})//内边距
.backgroundColor('#F6F6F6').width('100%')
Image($r('app.media.qgt1')).width('22').margin({left:10})//外边距
}.alignContent(Alignment.Start)//从左开始
Column() {
List() {
ListItem() {
Flex({ justifyContent: FlexAlign.SpaceBetween }) {
Image($r('app.media.qgt3')).width(35).margin(20)
Text('请党放心强国有我').fontSize(20).margin(30)
Text("13.14")
}.width('100%').backgroundColor(Color.White)
}
}
List() {
ListItem() {
Flex({ justifyContent: FlexAlign.SpaceBetween }) {
Image($r('app.media.qgt4')).width(35).margin(20)
Text('请党放心强国有我').fontSize(20).margin(30)
Text("13.14")
}.width('100%').backgroundColor(Color.White)
}
}
List() {
ListItem() {
Flex({ justifyContent: FlexAlign.SpaceBetween }) {
Image($r('app.media.qgt5')).width(35).margin(20)
Text('请党放心强国有我').fontSize(20).margin(30)
Text("13.14")
}.width('100%').backgroundColor(Color.White)
}
}
List() {
ListItem() {
Row() {
Text(' ').fontSize(20).height(400)
}.width('100%')
}
}
}.backgroundColor('#F6F6F6')
}.justifyContent(FlexAlign.SpaceBetween).padding(10)
}.height('100%')
}.tabBar(this.tablight('强国通',0,$r('app.media.xxqg1yes'),$r('app.media.xxqg1')))
//强国通结束
//百灵开始
TabContent(){
Column(){
Row(){
Stack(){
Row(){
Image($r('app.media.xxqg0')).width('18%').margin({bottom:10})
Stack(){
Stack(){
TextInput({placeholder:'强国同学汇'}).fontColor('#E8B0A5')
.backgroundColor('#E66A5E').width(155).padding({left:20})
Image($r('app.media.sousuo')).width('6%')
}.alignContent(Alignment.Start)
Image($r('app.media.saoma')).width('6%')
}.alignContent(Alignment.End)
Column(){
Text('积').fontSize(10).fontColor('#E8B0A5')
Text('分').fontSize(10).fontColor('#E8B0A5')
}.margin({left:15})
Text('19627').fontColor('#E8B0A5').margin({left:6})
Button('我的',{type:ButtonType.Normal}).onClick(()=>{
router.pushUrl({url:'pages/Pagewode'})
}).fontColor('#E8B0A5').margin({left:10}).backgroundColor('#E42417')
}.backgroundColor('#E42417').width('100%')
}
}
Tabs({
barPosition:BarPosition.End,controller:this.tab2
}){
TabContent(){
}.tabBar(this.TabBu('推荐',0))
TabContent(){
}.tabBar(this.TabBu('关注',1))
TabContent(){
}.tabBar(this.TabBu('党史',2))
TabContent(){
}.tabBar(this.TabBu('熊猫',3))
TabContent(){
}.tabBar(this.TabBu('美食',4))
}.height(40).margin({bottom:-20}).backgroundColor('#FDF2F0').height(50)
Divider().color(Color.Gray).margin(10).height(20)
//
Scroll(this.scroller){
Row(){
Column(){
Row(){
Flex({justifyContent:FlexAlign.Start}) {
Image($r('app.media.t1')).width('35').height('30')
.margin({ top: -15 })
Column() {
Flex({justifyContent:FlexAlign.Start}) {
Text('#追光2024#').margin({ top: -15 })
}
Flex({justifyContent:FlexAlign.Start}) {
Text('2024,开启年度追光之旅').fontColor(Color.Gray).fontSize(12)
}
}.margin({left:7})
}
}.width(200)
Row(){
Flex({justifyContent:FlexAlign.Start}) {
Image($r('app.media.t3')).width('30').height('30')
.margin({ top: 15 })
Column() {
Text('#科学同行 向新发力#').margin({ top: 15 })
Text('2024年中央企业科普作品展...').fontColor(Color.Gray).fontSize(12)
}
}
}.width(200)
// }
}.width(230)
Column(){
Row(){
Flex({justifyContent:FlexAlign.Start}) {
Image($r('app.media.t2')).width('30').height('30')
.margin({ top: -15 ,left:5})
Column() {
Text('#实用知识指南#').margin({ top: -15 ,left:5})
Text('哪些事儿你还不知道?').fontColor(Color.Gray).fontSize(12)
.margin({left:5})
}.margin({left:5})
}
}.margin({left:-60})
Row(){
Image($r('app.media.t4')).width('30').height('30')
.margin({ top: 15 ,left:10})
Column(){
Text('#强国领略#').margin({ top: 15 ,left:-65})
Text('跟着强国领阅达人的推荐一起...').fontColor(Color.Gray).fontSize(12)
.margin({left:10})
}
}.margin({left:-20})
}
}.height(120)
}.scrollable(ScrollDirection.Horizontal).scrollBarColor(Color.Red)
//
.scrollBarWidth(2)
Divider().color(Color.Gray).margin({top:-5})
Scroll(this.scroller){
Image($r('app.media.xxqgM'))
}.scrollable(ScrollDirection.Vertical).scrollBarColor(Color.Red)
.scrollBarWidth(2)
}.height('100%')
}.tabBar(this.tablight('百灵',1,$r('app.media.xxqg2yes'),$r('app.media.xxqg2no')))
//百灵结束
//学习开始
TabContent(){
Column(){
Row(){
Stack(){
Row(){
Image($r('app.media.xxqg0')).width('18%').margin({bottom:10})
Stack(){
Stack(){
TextInput({placeholder:'强国同学汇'}).fontColor('#E8B0A5')
.backgroundColor('#E66A5E').width(155).padding({left:20})
Image($r('app.media.sousuo')).width('6%')
}.alignContent(Alignment.Start)
Image($r('app.media.saoma')).width('6%')
}.alignContent(Alignment.End)
Column(){
Text('积').fontSize(10).fontColor('#E8B0A5')
Text('分').fontSize(10).fontColor('#E8B0A5')
}.margin({left:15})
Text('19627').fontColor('#E8B0A5').margin({left:6})
Button('我的',{type:ButtonType.Normal}).onClick(()=>{
router.pushUrl({url:'pages/Pagewode'})
}).fontColor('#E8B0A5').margin({left:10}).backgroundColor('#E42417')
}.backgroundColor('#E42417').width('100%')
}
}
Scroll(this.scroller) {
Column(){
Scroll(this.scroller) {
Row() {
ForEach(this.arr, (item: string) => {
Text(item.toString()).width('20%').height(100).backgroundColor('#FDF2F0').height(50)
})
}
}.scrollBar(BarState.On).scrollBarColor(Color.Red)
.scrollable(ScrollDirection.Horizontal)
Column() {
Swiper(this.swiperController) {
Image($r('app.media.xi')).width('100%')
Image($r('app.media.kaifang')).width('100%')
Image($r('app.media.kecheng')).width('100%')
}.autoPlay(true).loop(true)
Swiper(this.swiperController) {
Text('中共中央政治局召开民主生活会 习近平主持会议并发表重要讲话')
.fontColor(Color.Black)
.height(50)
Text('2024,总书记同人民在一起|开放促共赢').fontColor(Color.Black).height(50)
Text('科级及以下干部、党员学习贯彻党的二十届三中全会精神专题辅导系列课程')
.fontColor(Color.Black)
.height(50)
}.autoPlay(true).loop(true)
Row() {
Column() {
Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap }) {
Text('国家主席习近平将发表二〇二五年新年贺词').fontColor(Color.Black)
Text('"学习强国"学习平台 2024-12-30').fontSize(10).fontColor(Color.Gray)
}.width(340)
}
}.width('100%').padding(10)
Row() {
Image($r('app.media.a')).width('100%').padding(10)
}
Row() {
Column() {
Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap }) {
Text('每日金句(朗读版)|习近平论“一国两制”').fontColor(Color.Black)
Text('"学习强国"学习平台 2024-12-30').fontSize(10).fontColor(Color.Gray)
}.width(340)
}
}.width('100%').padding(10)
Row() {
Image($r('app.media.b')).width('100%').padding(10)
}
Row() {
Column() {
Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap }) {
Text('习近平就吉米 卡特逝世向美国总统拜登致电').fontColor(Color.Black)
Text('"学习强国"学习平台 2024-12-30').fontSize(10).fontColor(Color.Gray)
}.width(340)
}
}.width('100%').padding(10)
Row() {
Image($r('app.media.d')).width('100%').padding(10)
}
Row() {
Column() {
Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap }) {
Text('头条英文播报|Xi extends condolences over plane crash South Korea')
.fontColor(Color.Black)
Text('"学习强国"学习平台 2024-12-30').fontSize(10).fontColor(Color.Gray)
}.width(340)
}
}.width('100%').padding(10)
Row() {
Image($r('app.media.c')).width('100%').padding(10)
}
Row() {
Column() {
Flex({ justifyContent: FlexAlign.SpaceBetween, wrap: FlexWrap.Wrap }) {
Text('深入学习领会习近平文化思想的权威辅导读物').fontColor(Color.Black)
Text('"学习强国"学习平台 2024-12-30').fontSize(10).fontColor(Color.Gray)
}.width(340)
}
}.width('100%').padding(10)
}
}
}}
}.tabBar(this.tablight('',2,$r('app.media.xxqg3yes'),$r('app.media.xxqg3no')))
//学习结束
//电视台开始
TabContent() {
Column() {
Row(){
Stack(){
Row(){
Image($r('app.media.xxqg0')).width('18%').margin({bottom:10})
Stack(){
Stack(){
TextInput({placeholder:'强国同学汇'}).fontColor('#E8B0A5')
.backgroundColor('#E66A5E').width(155).padding({left:20})
Image($r('app.media.sousuo')).width('6%')
}.alignContent(Alignment.Start)
Image($r('app.media.saoma')).width('6%')
}.alignContent(Alignment.End)
Column(){
Text('积').fontSize(10).fontColor('#E8B0A5')
Text('分').fontSize(10).fontColor('#E8B0A5')
}.margin({left:15})
Text('19627').fontColor('#E8B0A5').margin({left:6})
Button('我的',{type:ButtonType.Normal}).onClick(()=>{
router.pushUrl({url:'pages/Pagewode'})
}).fontColor('#E8B0A5').margin({left:10}).backgroundColor('#E42417')
}.backgroundColor('#E42417').width('100%')
}
}
Tabs({
barPosition:BarPosition.End,controller:this.tab2
}){
TabContent(){
}.tabBar(this.TabBu('第一频道',0))
TabContent(){
}.tabBar(this.TabBu('视野',1))
TabContent(){
}.tabBar(this.TabBu('联播频道',2))
TabContent(){
}.tabBar(this.TabBu('看电视',3))
TabContent(){
}.tabBar(this.TabBu('看电台',4))
}.height(40).backgroundColor('#FDF2F0').height(50)
List({ space: 10 }) {
ListItem() {
Column() {
List(){
ListItem(){
Scroll(this.scroller) { //1滚动的内容2如何排
Row() { //开始排
Button('全部', { type: ButtonType.Normal, stateEffect: true }).backgroundColor(Color.Red).fontColor(Color.White)
Button('学习新视野', { type: ButtonType.Normal, stateEffect: true }).backgroundColor(Color.Red).fontColor(Color.White)
Button('奋斗新时代', { type: ButtonType.Normal, stateEffect: true }).backgroundColor(Color.Red).fontColor(Color.White)
Button('重要活动', { type: ButtonType.Normal, stateEffect: true }).backgroundColor(Color.Red).fontColor(Color.White)
Button('重要讲话', { type: ButtonType.Normal, stateEffect: true }).backgroundColor(Color.Red).fontColor(Color.White)
Button('重要文章', { type: ButtonType.Normal, stateEffect: true }).backgroundColor(Color.Red).fontColor(Color.White)
}
}.scrollBar(BarState.On).scrollBarColor(Color.White).scrollBarWidth('5%')//滑动条是否存在,~颜色
.scrollable(ScrollDirection.Horizontal).scrollBarWidth(2)//横向滑动条
}
}
Image($r('app.media.a6')).width('80%').height('30%')
Flex({ direction: FlexDirection.Column })
Text('中共中央政治局召开民主生活会近平主持会议并发表重要讲话').fontSize('20')
Text('中央广播电视总台 2024-12-27').fontSize('13')
}
}
ListItem() {
Column() {
Image($r('app.media.a3'))
Text('习近平向中吉乌铁路项目启动仪式致贺信').fontSize('20')
Text('中央广播电视总台 2024-12-27').fontSize('13')
}
}
ListItem() {
Column() {
Image($r('app.media.a4'))
Text('习近平向中吉乌铁路项目启动仪式致贺信')
.fontSize('20')
}
}
ListItem() {
Column(){
Image($r('app.media.a2'))
Text('习近平就吉米卡特·卡特逝世向美国总统致唁电').fontSize('20')
}
}
ListItem() {
Column(){
Image($r('app.media.a1'))
Text('习近平就韩国客机失事向韩国代总统致慰问电').fontSize('20')
}
}
ListItem() {
Text('下面的').fontSize(24).height('30%')
}
ListItem() {
Text('就别看了').fontSize(24).height('30%')
}
ListItem() {
Text('太多了').fontSize(24).height('30%')
}
ListItem() {
Text('不想写了').fontSize(24).height('30%')
}
ListItem() {
Text('就这样吧').fontSize(24).height('30%')
}
ListItem() {
Text('累了').fontSize(24).height('30%')
}
ListItem() {
Text('啊吧啊吧啊吧吧').fontSize(24).height('30%')
}
}.divider({
strokeWidth: 1,
startMargin: 60,
endMargin: 10
})
.backgroundColor('#FFF1F3F5')
.alignListItem(ListItemAlign.Center)
}.height('100%')
}.tabBar(this.tablight('电视台', 3, $r('app.media.xxqg4yes'), $r('app.media.xxqg4no')))
//电视台结束
//电台开始
TabContent(){
Column(){
Row(){
Stack(){
Row(){
Image($r('app.media.xxqg0')).width('18%').margin({bottom:10})
Stack(){
Stack(){
TextInput({placeholder:'强国同学汇'}).fontColor('#E8B0A5')
.backgroundColor('#E66A5E').width(155).padding({left:20})
Image($r('app.media.sousuo')).width('6%')
}.alignContent(Alignment.Start)
Image($r('app.media.saoma')).width('6%')
}.alignContent(Alignment.End)
Column(){
Text('积').fontSize(10).fontColor('#E8B0A5')
Text('分').fontSize(10).fontColor('#E8B0A5')
}.margin({left:15})
Text('19627').fontColor('#E8B0A5').margin({left:6})
Button('我的',{type:ButtonType.Normal}).onClick(()=>{
router.pushUrl({url:'pages/Pagewode'})
}).fontColor('#E8B0A5').margin({left:10}).backgroundColor('#E42417')
}.backgroundColor('#E42417').width('100%')
}
}
Scroll(this.scroller) {
Flex({ justifyContent: FlexAlign.SpaceAround }) {
Text('听同期声').margin({ top: 15 }).width('25%')
Text('听原著').margin({ top: 15 }).width('25%')
Text('强国之声').margin({ top: 15 }).width('25%')
Text('听广播').margin({ top: 15 }).width('25%')
Text('听新闻报摘').margin({ top: 15 }).width('25%')
Text('听历史').margin({ top: 15 }).width('25%')
Text('听理论').margin({ top: 15 }).width('25%')
Text('云听').margin({ top: 15 }).width('25%')
Text('听法律').margin({ top: 15 }).width('25%')
Text('听文化').margin({ top: 15 }).width('25%')
}.backgroundColor('#FDF2F0').height(50)
}.scrollBar(BarState.On).scrollBarColor(Color.Red)//滑动条是否存在,~颜色
.scrollBarWidth('15%')
.scrollable(ScrollDirection.Horizontal)//横向滑动条
Scroll(this.scroller) {
Row() {
Image($r('app.media.dt1'))
}
}.scrollBar(BarState.Off)
}.height('100%')
}.tabBar(this.tablight('电台',4,$r('app.media.xxqg5yes'),$r('app.media.xxqg5no')))
}.barWidth('100%').barHeight(40)
}.height('100%')
//电台结束
}
}
}
}
Pageshezhi.ets
import { router } from '@kit.ArkUI';
@Entry
@Component
export struct Pageshezhi {
@State message: string = 'Hello World';
build() {
Column() {
Column() {
Row() {
Image($r('app.media.My_2')).onClick(() => {
console.log('返回成功')
router.back()
}).width(30).margin({ right: 300 })
}
Column(){
Image($r('app.media.My_tc')).width('100%')
}
Column() {
Button('退出登录',{type:ButtonType.Normal}).onClick(()=>{
router.pushUrl({url:'pages/Pagedelu'})
}).backgroundColor(Color.Transparent)
.fontColor(Color.Black)
}
}
}
}
}
Pagesdelu.ets
import router from '@ohos.router';
import http from '@ohos.net.http';
let httpReq = http.createHttp()
@Entry
@Component
export struct Pagexuexi {
@State message: string = 'Hello World';
@State name:string=''
@State password:string=''
post(action = 'login',name:string,password:string){
httpReq.request('http://localhost:8087/untitleda_war_exploded/login.do',{
method:http.RequestMethod.POST,
header:{'Content-Type':'application/json'},
extraData:{name,password},
expectDataType:http.HttpDataType.OBJECT
}).then((res:object)=>{
if (JSON.stringify(res).indexOf('succ')>-1) {
router.pushUrl({url:'pages/Index'})
}
})
}
build() {
Column(){
Row(){
Image($r('app.media.xuexi')).width(200).margin({left:-40})
Image($r('app.media.touxiang')).width(45).margin({left:60})
}
Column(){
Text('手机号码').padding({right:220}).margin({bottom:20})
Row(){
Column(){
Image($r('app.media.shuzi')).width(100).margin({left:40})
}
Column(){
TextInput({placeholder:'请输入手机号码'}).backgroundColor(Color.White).onChange((value)=>{this.name=value})
}
}
}.margin({top:100})
Divider().height(20).foregroundColor(Color.Gray).padding({bottom:20})
Row(){
Text('密码').padding({right:240}).margin({bottom:20})
}
Row(){
TextInput({placeholder:'请输入密码'}).backgroundColor(Color.White).type(InputType.Password).onChange((value)=>{this.password=value})
}.margin({left:30})
Divider().height(20).foregroundColor(Color.Gray).padding({bottom:20})
Row(){
Button('登录',{type:ButtonType.Normal}).onClick(()=>{
this.post('login',this.name,this.password)
}).width(270).backgroundColor(Color.Pink)
}.margin({bottom:20})
Row(){
Button('新用户注册',{type:ButtonType.Normal}).onClick(()=>{
console.log('开始注册')
router.pushUrl({url:'pages/Pagezhuce'})
}).width(270).backgroundColor(Color.White).fontColor(Color.Red)
}
Row(){
Column(){
Text('找回密码').margin({right:50})
}
Column(){
Text('账号挂失')
}
}.padding({top:180})
}
}
}
Pagewode.ets
import router from '@ohos.router'
@Entry
@Component
export struct Index {
@State message: string = 'Hello World';
scroller: Scroller = new Scroller()
build() {
Column() {
Stack({ alignContent: Alignment.TopStart }) {
Column() {
Row() {
Row(){
Button('返回')
.onClick(()=>{
console.log('返回成功')
router.back()
}).width(60).margin({right:300})
.backgroundColor(Color.Transparent)
.fontColor(Color.Black)
.margin({left:-70})
}
Row() {
Button('设置',{type:ButtonType.Normal}).onClick(()=>{
router.pushUrl({url:'pages/Pageshezhi'})
}).backgroundColor(Color.Transparent)
.fontColor(Color.Black)
}.margin({right:-70})
}.backgroundColor(Color.Red).justifyContent(FlexAlign.SpaceAround).width('100%')
}.justifyContent(FlexAlign.Start).backgroundColor(':#fC4F3F')
}
Column() {
Image($r('app.media.My_b')).width('100%')
}
//图片结束
Column({ space: 5 }) {
Flex({ justifyContent: FlexAlign.Start }) {
Text('强国服务').fontSize(18).margin({ left: 20 })
}
}
Row() {
Column({ space: 5 }) {
Image($r('app.media.My_4')).width(48)
Text('同学汇').fontSize(13)
}
Column({ space: 5 }) {
Image($r('app.media.My_5')).width(48)
Text('报告').fontSize(13)
}
Column({ space: 5 }) {
Image($r('app.media.My_6')).width(48)
Text('京东精选').fontSize(13)
}
Column({ space: 5 }) {
Image($r('app.media.My_20q')).width(48)
Text('汽车询价').fontSize(13)
}
}.width('100%').justifyContent(FlexAlign.SpaceAround)
}
}
}
java后端
配置pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.1</version>
</dependency>
</dependencies
配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>E</servlet-name>
<servlet-class>com.util.controller.EController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>E</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
连接数据库dbt.java
package com.util;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
public class dbt {
public static Connection getCon() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/nc1?serverTimezone=UTC","root","123456");;
return con;
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
System.out.println(dbt.getCon());
}
}
配置文件E
package com.util;
import java.util.HashMap;
import java.util.Map;
public class E {
public static void main(String[] args) {
Map<String,Integer> mm=new HashMap<>();
mm.put("age",19);
mm.put("hi",180);
System.out.println(mm.get("age"));
}
}
uses.dao
package com.util.dao;
import com.util.dbt;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class userdao {
public void insert(String name,String password) throws SQLException, ClassNotFoundException {
//1 连接
//2 预处理
//3 执行
Connection con = dbt.getCon();
String sql = "insert into user(name,password) value(?,?)";
PreparedStatement ps = con.prepareStatement(sql);//预处理
ps.setString(1,name);
ps.setString(2,password);
int x = ps.executeUpdate();//执行
System.out.println(x);
}
//login
public boolean login(String name,String password) throws SQLException, ClassNotFoundException {
Connection con = dbt.getCon();
String sql="select * from user where name=? and password=?";
PreparedStatement ps = con.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,password);
ResultSet rs = ps.executeQuery();
return rs.next();
}
public static void main(String[] args) throws SQLException, ClassNotFoundException {
userdao ud = new userdao();
boolean b = ud.login("song2","123456");
// ud.insert("song2","123456");
System.out.println(b);
// if(b){
// System.out.println("login succ");
// }
// else {
// System.out.println("login false");
// }
}
}
连接前后端Econtroller.java
package com.util.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sun.xml.internal.bind.v2.model.core.BuiltinLeafInfo;
import com.util.dao.userdao;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Map;
public class EController extends HttpServlet {
//处理 get post请求
//req 请求 res 相应--重写
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("servlet hello");
//先把编号方式设置
req.setCharacterEncoding("UTF-8");
resp.setContentType("Text/html;charset=UTF-8");
//区分中login.do 还是reg.do list.do
String uri = req.getRequestURI();
//取内容 从 到 哪
uri = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
System.out.println(uri);//login
//调用dao
userdao ud = new userdao();
if (uri.equals("/login")){
System.out.println("ets");
BufferedReader reader = req.getReader();
System.out.println(reader);
String line;//line负责用String来接收装好的流,一杯一杯的装
String line2="";
while ((line=reader.readLine())!=null){
line2 = line2 +line +"\n";
}
System.out.println(line2);
ObjectMapper om = new ObjectMapper();
Map<String,Object> mm=om.readValue(line2,Map.class);
String name=(String)mm.get("name");
String password=(String)mm.get("password");
System.out.println(name);
System.out.println(password);
try {
boolean bo = ud.login(name,password);
if (bo){
//PrintWriter可以把想输出的内容,在当前链接下
PrintWriter pr=resp.getWriter();
//{"mess":"succ"}
pr.println("{\"mess\":\"succ\"}");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
else if (uri.equals("/reg")){
System.out.println("reg");
}
}
}